Difficulty: Medium
Correct Answer: char *ptr[3];
Explanation:
Introduction / Context:
Translating English descriptions into precise C declarations is a core skill for systems programming. The target phrase “an array of three pointers to chars” involves both array and pointer declarators, and it is easy to overcomplicate the syntax by adding function parentheses or extra indirection.
Given Data / Assumptions:
Concept / Approach:
Build from the inside out. The element type is “pointer to char”, written as char*. An array of 3 such elements is written by attaching [3] to the identifier: char *ptr[3]; Parentheses are not needed around the * for this simple case. Adding () would convert it into “array of pointers to functions”, which is not requested.
Step-by-Step Solution:
Verification / Alternative check:
Use sizeof to confirm: sizeof(ptr) equals 3 * sizeof(char*). The expression *ptr[i] then has type char, confirming that each element is a pointer to char, not a function or a pointer to pointer.
Why Other Options Are Wrong:
char *ptr[3](); Declares an array of 3 functions returning char*, which is illegal because arrays of functions are not allowed; with some compilers it is read as array of 3 pointers to functions returning char* due to decay, which still does not match.
char (*ptr[3])(); Explicitly an array of 3 pointers to functions, not pointers to char.
char **ptr[3]; An array of 3 pointers to pointer to char (char**), which is one extra level of indirection.
Common Pitfalls:
Final Answer:
char *ptr[3];
Discussion & Comments