Skip to content

Add Example of pointer to Functions #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ These program are written in codeblocks ide for windows. These programs are not
- [Recursion](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Recursion.c)
- [Segmentation Fault or Bus Error Demo](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SegmentationFaultorBusErrorDemo.c)
- [Structure](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Structure.c)
- [Basic Pointers to Functions](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/basicFunctionPointers.c)
- [Swapping 2 Numbers Without a Third Variable or ^](https://github.com/geetanjaliaich/beginners-C-program-examples/blob/FactorialEratosthenes/SwapIntegersWithout3rdVariable(Arithmatic).c)
- [Print 100 Prime numbers using Seive of Eratosthenes](https://github.com/geetanjaliaich/beginners-C-program-examples/blob/FactorialEratosthenes/PrimeByEratosthenes.c)
- [Palindrome Number](https://github.com/geetanjaliaich/beginners-C-program-examples/blob/FactorialEratosthenes/PalindromeNumber.c)
Expand Down
27 changes: 27 additions & 0 deletions basicFunctionPointers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<stdio.h>
/**
* add - adds two numbers
* this function would be called indirectly with function pointers
* @a: integer
* @b: integer
* Return: sum of a and b
*/
int add(int a, int b)
{
return (a + b);
}

/**
* main - entry point
* the main function calls the add function indirectly using function pointers
* Return: 0 (success)
*/
int main(void)
{
int (*fptr)(int, int); /* This is the declaration of the function pointer.
this pointer points to a function that takes two integers
as arguments and return an integer */

fptr = add;
printf("%d", fptr(1,2));
}