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

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

Pranathi said:   1 decade ago
Anybody please clarify.

int (*p)() = fun;

(*p)() ;

How these two lines will work?

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

This must be the right usage.

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

Jyothi said:   1 decade ago
What is enum error?

Akshay said:   1 decade ago
Thanks Rupinder.

Silpa said:   9 years ago
Thanks @Sandeep.

Dhara said:   8 years ago
Thanks @Sandeep.

Hariprasad said:   7 years ago
Thanks @Sandeep.

Mana said:   1 decade ago
Thanks sandeep.


Post your comments here:

Your comments will be displayed after verification.