GATE | GATE-CS-2005 | Question 1
What does the following C-statement declare? [1 mark]
(A) A function that takes an integer pointer as argument and returns an integer.
(B) A function that takes an integer as argument and returns an integer pointer.
(C) A pointer to a function that takes an integer pointer as argument and returns an integer.
(D) A function that takes an integer pointer as argument and returns a function pointer
Explanation: The steps to read complicated declarations :
1)Convert C declaration to postfix format and read from left to right.
2)To convert expression to postfix, start from innermost parenthesis, If innermost parenthesis is not present then start from declarations name and go right first. When first ending parenthesis encounters then go left. Once the whole parenthesis is parsed then come out from parenthesis.
3)Continue until complete declaration has been parsed.
At First, we convert the following given declaration into postfix:
int ( * f) (int * )
Since there is no innermost bracket, so first we take declaration name f, so print “f” and then go to the right, since there is nothing to parse, so go to the left. There is * at the left side, so print “*”.Come out of parenthesis. Hence postfix notation of given declaration can be written as follows:
f * (int * ) int
Meaning: f is a pointer to function (which takes one argument of int pointer type) returning int .
This solution is contributed by Nirmal Bharadwaj.
Quiz of this Question