C declarations: write a declaration for "An array of three pointers to chars" Choose the correct C syntax that matches the English statement.

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:

  • We want exactly 3 elements.
  • Each element must be a pointer to char, that is, type char*.
  • No functions are involved, no extra levels of pointers beyond one.


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:

1) Decide the element type: pointer to char ⇒ char*. 2) Make an array of 3 such elements ⇒ char *ptr[3]; 3) Confirm that ptr[i] has type char* for i in {0,1,2}. 4) No function declarators are present, so no call syntax appears.


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:

  • Adding function parentheses by habit, creating function pointer declarations unintentionally.
  • Confusing char* (pointer to char) with char** (pointer to pointer to char).


Final Answer:

char *ptr[3];

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion