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 1 of 3.
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.
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();
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.
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.
How fun is said as a function?
Please clear me I can't get.
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.
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.
Sathish 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?
Poonam said:
1 decade ago
Its not clear completely because *p()=fun doesn't specify that fun is a variable or a function name.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers