C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Point Out Errors (Q.No. 5)
5.
Point out the error in the following program.
#include<stdio.h>
int main()
{
    int (*p)() = fun;
    (*p)();
    return 0;
}
int fun()
{
    printf("IndiaBix.com\n");
    return 0;
}
Error: in int(*p)() = fun;
Error: fun() prototype not defined
No error
None of these
Answer: Option
Explanation:

The compiler will not know that the function int fun() exists. So we have to define the function prototype of int fun();
To overcome this error, see the below program


#include<stdio.h>
int fun(); /* function prototype */

int main()
{
    int (*p)() = fun;
    (*p)();
    return 0;
}
int fun()
{
    printf("IndiaBix.com\n");
    return 0;
}
Discussion:
21 comments Page 3 of 3.

Sathish said:   1 decade ago
Anybody please clarify.

int (*p)() = fun;

(*p)() ;


How these two lines will work?


Post your comments here:

Your comments will be displayed after verification.