How to declare a pointer to a function in C?

C++Server Side ProgrammingProgramming

A pointer is a variable whose value is the address of another variable or memory block, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable or block address.

Syntax

Datatype *variable_name

Algorithm

Begin.
   Define a function show.
      Declare a variable x of the integer datatype.
      Print the value of varisble x.
   Declare a pointer p of the integer datatype.
   Define p as the pointer to the address of show() function.
   Initialize value to p pointer.
End.

This is a simple example in C to understand the concept a pointer to a function.

#include
void show(int x)
{
   printf("Value of x is %d\n", x);
}
int main()
{
   void (*p)(int); // declaring a pointer
   p = &show; // p is the pointer to the show()
   (*p)(7); //initializing values.
   return 0;
}

Output

Value of x is 7.
raja
Updated on 30-Jul-2019 22:30:25

Advertisements