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;
}
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.
Nithin said:
1 decade ago
@Anu the parenthesis indicate that the p points to a function named fun. For variable we'll not include parenthesis.
Anu said:
1 decade ago
What is the need of extra parenthesis in the statement after pointer variable and does fun contain any parenthesis?
int (*p)()=fun;
int (*p)()=fun;
Surya said:
1 decade ago
Function prototype is optional for functions having return type of int. So I think function prototype is not necessary.
Akshay said:
1 decade ago
Thanks Rupinder.
Rupinder said:
1 decade ago
(*p)().......*p is a pointer variable points to function because in declaration we have () brackets.Since, it points to function named "fun" and holds it's address.so to call function which is at particular address in memory,which is hold by pointer variable p(pointer to function),we can use it to make a call by using specific typecast of function pointer.
Jyothi said:
1 decade ago
What is enum error?
Mana said:
1 decade ago
Thanks sandeep.
Poonam said:
1 decade ago
Its not clear completely because *p()=fun doesn't specify that fun is a variable or a function name.
Sandeep said:
1 decade ago
int (*p)() = fun assigns a pointer to the function named fun.
as you already know int *p=a; assigns a pointer to the variable a.
in the similar way this also works.
and then (*p)() is a call to the function fun() which is similar to direct call as
fun();
as you already know int *p=a; assigns a pointer to the variable a.
in the similar way this also works.
and then (*p)() is a call to the function fun() which is similar to direct call as
fun();
Pranathi said:
1 decade ago
Anybody please clarify.
int (*p)() = fun;
(*p)() ;
How these two lines will work?
int (*p)() = fun;
(*p)() ;
How these two lines will work?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers