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 1 of 3.

Leela said:   7 years ago
int (*p)()=fun; // this statement is pointed as nested function, and shown an error, HOW?
How fun is said as a function?
Please clear me I can't get.

Hariprasad said:   7 years ago
Thanks @Sandeep.

SATHYA said:   7 years ago
int (*p)()=&fun;.

This must be the right usage.

Surendra said:   8 years ago
Thanks you all.

Dhara said:   8 years ago
Thanks @Sandeep.

Silpa said:   9 years ago
Thanks @Sandeep.

Mariyan said:   9 years ago
Thanks a lot friends for clarifying the answers.

Rohi said:   9 years ago
I compiled the code given in answers on ideone compiler and got errors. How to clear the error?

Shrruti said:   1 decade ago
In declaration we have a pair of brackets with *p. Thats why it treats fun as function. If we eliminate brackets then it will be treated as variable. Eg- *p=a.

Akash said:   1 decade ago
In this case won't the error be reported on the line "int (*p)() = fun;" as the compiler will figure 'fun' as a variable rather than a function.


Post your comments here:

Your comments will be displayed after verification.