Declaration of a function pointer
Syntax
return type (*ptr_name)(type1, type2…);

You can also try this code with Online C Compiler
Run Code
Example
int (*ip) (int);

You can also try this code with Online C Compiler
Run Code
In the above declaration, *ip is a pointer that points to a function that returns a value of type int and accepts an integer value as an argument.
float (*fp) (float);

You can also try this code with Online C Compiler
Run Code
In the above declaration, *fp is a pointer that points to a function that returns a float value and accepts a float value as an argument.
We will observe that the declaration of a function is much like a function pointer except that the Pointer is preceded by a '*.' So, within the above declaration, fp is declared as a function rather than a function pointer.
Calling a function
By using a function pointer, we can also use the name of a function to call it. The syntax for calling the function through the function pointer is similar to calling it directly.
Example
include <iostream>
using namespace std;
void printname(char *name)
{
std::cout << "Name:" <<name<< std::endl;
}
int main()
{
char x[30];
void (*ptr)(char*);
ptr=printname;
std::cout << "Enter name: " << std::endl;
cin>>x;
cout<<x<<endl;
ptr(x); // calling printname() function
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter name:
CodingNinjas
CodingNinjas
Name: CodingNinjas

You can also try this code with Online C Compiler
Run Code
In this example, we explain calling a function.
Address of a function
To get an address of a function, we need first to state the function's name. There is no want for us to call the function.
Example
#include <iostream>
using namespace std;
int main()
{
std::cout << "The address of function main(): " <<&main<< std::endl;
return 0;
}

You can also try this code with Online C Compiler
Run Code
In the above program, we are displaying the address of our main function while listing the name of the function with no parameters no brackets to print the address of the main() function. As a result, a function's address is surely its name without any brackets or parameters.
You can also read about the dynamic arrays in c, And Tribonacci Series
The Array of Function Pointers
Function pointers are used in applications where we do not know which function could be called earlier. In an array of function pointers, the array takes the addresses of different functions, and the appropriate function could be called based on the index number.
Example
#include <stdio.h>
int sum(int num1, int num2);
int sub(int num1, int num2);
int mult(int num1, int num2);
int div(int num1, int num2);
int main()
{
int x, y, choice, result;
int (*ope[4])(int, int);
ope[0] = sum;
ope[1] = sub;
ope[2] = mult;
ope[3] = div;
printf("Enter two integer numbers: ");
scanf("%d%d", &x, &y);
printf("Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: ");
scanf("%d", &choice);
result = ope[choice](x, y);
printf("%d", result);
return 0;}
int sum(int x, int y) {return(x + y);}
int sub(int x, int y) {return(x - y);}
int mult(int x, int y) {return(x * y);}
int div(int x, int y) {if (y != 0) return (x / y);
else return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter two integer numbers: 13 48
Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: 2
624

You can also try this code with Online C Compiler
Run Code
Also see, Short int in C Programming
FAQs
-
How to call a function without using the function name to send parameters?
Here are two ways to pass parameters in C: Pass by Value, Pass by Reference.
Pass by Value. Pass by Value, means that a copy of the data is made and stored by way of the name of the parameter. ...
Pass by Reference. A reference parameter "refers" to the original data in the calling function.
-
Which of the following is the correct syntax to pass a Function Pointer as an argument?
void pass(int (*fptr)(int, float, char)){}
Key Takeaways
In this article, we have extensively discussed function Pointer topics, and we learned in this blog we've long gone through function pointers. We've seen the importance of function pointers, in which they're used, and how they make our programs simpler to develop and maintain. We can hope that this blog has helped you enhance your knowledge regarding the function of Pointer, and if you would like to learn more, check out our articles on Coding Ninjas and visit our Library for more.
Check out this problem - Redundant Braces
Recommended Readings:
Do upvote our blog to help other ninjas grow.
"Happy Coding!"