From 4c70a296765a0fa367cbac181398ca8ee4076c87 Mon Sep 17 00:00:00 2001 From: Emmanuel Nwafor Date: Sun, 22 Jan 2023 18:41:15 +0100 Subject: [PATCH 1/2] add file to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 341baa9..0d058ca 100644 --- a/README.md +++ b/README.md @@ -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) From 06d720c14f8ddacbb3f912eca41bee01103cfc92 Mon Sep 17 00:00:00 2001 From: Emmanuel Nwafor Date: Sun, 22 Jan 2023 18:41:47 +0100 Subject: [PATCH 2/2] Example of pointer to functions in C --- basicFunctionPointers.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 basicFunctionPointers.c diff --git a/basicFunctionPointers.c b/basicFunctionPointers.c new file mode 100644 index 0000000..86afcbe --- /dev/null +++ b/basicFunctionPointers.c @@ -0,0 +1,27 @@ +#include +/** + * 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)); +} \ No newline at end of file