Question 6 Which among the following is the correct way to declare a pointer to a function...
IBM technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Which among the following is the correct way to declare a pointer to a function in C?
Pick ONE option
Show answer & explanation
In C, int (*ptr)(); declares a pointer to a function that takes no arguments and returns an int. The parentheses around *ptr are critical—they force the pointer operator to bind to ptr before the function call operator. Without them, the syntax declares something different: int *ptr(); declares a function returning a pointer to int, int *ptr; declares a pointer to int, and int *ptr[]; declares an array of pointers to int.
Step-by-step Derivation:
To declare a pointer to a function, use the pattern return_type (*pointer_name)(parameter_types);. The parentheses around *ptr are essential to establish that ptr is a pointer (not a function). Option A matches this pattern: int (*ptr)(); means 'ptr is a pointer to a function taking no arguments and returning int'. Option B int *ptr(); is a function declaration (reads as 'ptr is a function returning pointer to int'). Option C int *ptr; is a simple pointer to int. Option D int *ptr[]; is an array of pointers to int.