C Function Pointer

We announce a pointer to number, pointer to character or pointer to cluster.

Likewise, we can pronounce a pointer to function or a function pointer.

Same as like factors, functions likewise have some location in memory.

C Function Pointer

A function pointer is a pointer that contains the location of a function. It very well may

be pronounced in the following manner. You should pursue a similar

sentence structure for presentation else you will get a mistake.

Syntax

return_type (* pointer_name) (argument_list);

Example

int (*p) (int, int);

In the above example, we are announcing a pointer to function or a function pointer that

will contain the location of a function with return type whole number and having two whole number contentions.

The above function pointer p can be appointed location of a function and can call that function

in the following manner. Let’s expect the function with the name fun1().

//storing address of function in the function pointer p
p = fun1; //first way
p = &fun1 //second way
 
//calling the function using function pointer p
int c = (*p)(2,3); //first way
int c = p(2,3); //second way

As should be obvious in the above example that function pointer can be doled

out the address and can call a function in two different ways. You can utilize any of them.

Let’s make one program that will figure expansion of two numbers utilizing the idea of function pointer in C.

#include<stdio.h>
 
int add(int a,int b)
{
 return a+b;
}
 
void main()
{
 int (*p)(int,int);
 
 p=&add;
 printf("Sum=%dn",(*p)(5,3));  
 
}

Output:

Sum=8
Press any key to continue . . . _

Clarification

Right off the bat, I have made a function include() that will take two whole number

contentions, compute their expansion and return the outcome.

Presently in the primary() function, I am proclaiming a function pointer p and

afterwards putting away the location of function include(). This should be possible without utilizing the location of the administrator for example and.

At last, I am calling the function utilizing the function pointer and afterwards

showing the outcome. As I have just referenced over that the calling should likewise be possible

in two different ways. So here we can call the function by basically composing p(5,3).

Beneath I have included a video that will help you in understanding the idea of function pointer or pointer to function in C.

C function pointer is a development theme of pointer and somewhat troublesome as well.

In the event that you have any sort of questions, at that point please notice it in remarks.

Leave a Comment

error: Alert: Content is protected!!