Function Pointer in C

Function Returning Pointer

Consider a simple program :

main()
   {
    int *x;
    x=fun();
    printf("%u",x);
   }
int* fun()
   {
    int y=10;
    return &y;
   }
Output of the Program :
2000
Explanation :

Function Pointer in C - Function Returning Pointer

So, It means that function is returning a pointer.

Functional Pointer

Consider a simple program to understand the meaning of functional pointer.

main()
   {
    int display();
    int *fp();
    fp = display;
    (*fp)();
    (fp)();
   }
int display()
   {
    printf("\nHi I am the Display.");
    return(1);
   }
Output of the Program :
Hi I am the Display.
Hi I am the Display.
Explanation of Program :

Function Pointer in C - Functional Pointer

Points in Program 2 :

  • What does the int (*fp)(); means? ⇒ It means fp is a pointer points to function giving integer. or int (*fp)(); is calling a function address which will return integer.
  • The function fp() will store the address of a function.
  • Array name, Function name, and sting name represents their own address. Therefore, in a 16 bit processor, they are of 2-bytes.
  • So, if we use (fp = &display) instead of (fp = display) in line 5th, then there is no difference between these two statements as function name itself represents the address of the function.

Resemblance with simple Statements

Statement Number Statement in Function Simple Statements
 3 int display(); int a;
 4 int (*fp)(); int *b;
 5 fp = display *b = a;
 6 (*fp); *b;

Array of Function :

Consider a simple Program to understand the meaning of array of function :

main()
   {
    int (*ptr[3])();
    ptr[0] = aaa;
    ptr[1] = bbb;
    ptr[2] = ccc;
    (*ptr[2])();
   }
int aaa()
   {
    printf("aaa");
    return 0;
   }
int bbb()
   {
    printf("bbb");
    return 0;
   }
int ccc()
   {
    printf("ccc");
    return 0;
   }
Output of the Program :
ccc
Explanation of the Program :

Function Pointer in C - Array of function

Points in Program 3 :

  • What does the int (*ptr[3])(); means? ⇒ This statement means that ptr is an array of 3 pointers points to function which takes no parameter and finally return an integer value.
  • int (*ptr[3])(); will store the address of 3 functions.

What does the following C statement will declare?

  • int (*f)(int *);
    f is a pointer points to function which takes parameter of integer pointer which will finally return integer value.
  • int (*f)();
    f is a pointer points to function which takes no parameters and finally return integer value.
  • int *f()(int,int);
    f is a pointer points to function which takes two integer parameters and finally return integer value.
  • int* (*f)(int*, int*);
    f is a pointer points to function which takes two integers pointer as parameters and finally return an integer pointer.
  • void (*abc) (int, void(*def)())
    abc is a pointer to a function which will take two parameters as input. The first parameter is integer and second(def) is a pointer points to a function which will return void and the whole function will finally return void.
  • char* (*(*a[N])())();
    a is an array of N pointer points to a function which will return a pointer and that pointer points to function which finally return a character pointer.
  • int* (char* (*)()) (*ptr[N])();
    A pointer points to a function returns a character pointer pointing to an array N pointer ptr which points to a function and which will finally return a integer pointer.
    () () → Associativity will be Left → Right.
Question : Match the following :
1. A pointer to an array of 8 floats (a) float* (*f)()
2. A pointer to an array of 8 pointer to floats. (b) float (*f)()
3. A pointer to a function that returns a float. (c) float (*a)[8]
4. A pointer to a function that returns pointer to float (d) float* (*a)[8]
1 - (c)
2 - (d)
3 - (b)
4 - (a)
     

Leave a Reply