1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A variable must hold a pointer to a function taking an int…

Added 30 Nov -0001

A variable must hold a pointer to a function taking an int and returning int. Which declaration is right, and how does it compare with the alternative?

  1. int (*fp)(int); which means exactly the same thing as int *fp(int) under the C declarator precedence rules
  2. int fp(*)(int); which places the pointer inside the parameter list rather than on the name
  3. int (*fp)(int); which declares a function pointer, unlike int *fp(int) which declares a function Correct answer
  4. int *fp(int); which declares a function pointer, while int (*fp)(int) declares a function

Correct answer

C. int (*fp)(int); which declares a function pointer, unlike int *fp(int) which declares a function

16 views

MCQSEXAM

Explanation

Declarator precedence binds the parentheses around *fp first, so int (*fp)(int) is a pointer to function, while int *fp(int) declares a function returning int*. Reading a declaration inside out from the identifier resolves such puzzles quickly. The same rule explains why an array of function pointers is written int (*tab[3])(int).

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs