C declarations: interpret the following declaration char **argv;
-
Aargv is a pointer to pointer.
-
Bargv is a pointer to a char pointer.
-
Cargv is a function pointer.
-
Dargv is a member of function pointer.
Answer
Correct Answer: argv is a pointer to a char pointer.
Explanation
Introduction / Context:Understanding multi-level pointers is essential in C, especially for command-line arguments and arrays of strings. The identifier argv is widely used in main's formal parameter list as char *argv, representing an array of C strings where each element is a char and the array itself is addressed via a pointer to pointer to char.
Given Data / Assumptions:
- Declaration under analysis: char **argv;
- We are interpreting only the type, not storage duration or qualifiers.
- A typical use case is main(int argc, char **argv).
Concept / Approach:
char* means “pointer to char”. Adding another * yields char** which means “pointer to pointer to char”. This is commonly used for an array-of-pointers representation of strings, where argv points to the first element of an array of char* values, each of which points to the first character of a NUL-terminated string.
Step-by-Step Solution:
1) Recognize ** as two levels of indirection. 2) The outer pointer refers to an element of type char*. 3) Each char* then points to a sequence of char ending with '\\0'. 4) Therefore argv is “pointer to a char pointer”.Verification / Alternative check:
Indexing argv like argv[i] produces type char*, and dereferencing once *argv[i] yields char, consistent with a list of strings. This aligns with the standard practice of treating argv as an array of pointers to C strings.
Why Other Options Are Wrong:
Pointer to pointer — Too vague; it does not specify the base type char.
Function pointer — No function declarator () is present.
Member of function pointer — Nonsensical in C terminology.
Common Pitfalls:
- Assuming char** is interchangeable with char*[]. They are related through array-to-pointer decay but not identical types in all contexts.
- Forgetting to allocate and terminate the array of pointers when constructing argv-like structures.
Final Answer:
argv is a pointer to a char pointer.