What does the following declaration mean ? int (*ptr) [10];
2013
What does the following declaration mean ?
int (*ptr) [10];
- A.
ptr is an array of pointers of 10 integers.
- B.
ptr is a pointer to an array of 10 integers.
- C.
ptr is an array of 10 integers.
- D.
none of the above.
Attempted by 329 students.
Show answer & explanation
Correct answer: B
Key point: int (*ptr)[10]; declares ptr as a pointer to an array of 10 integers.
How to read it: parentheses bind the * to ptr first, so (*ptr) has type int (*)[10], i.e., pointer to int[10].
Contrast with similar declarations:
int *ptr[10]; declares an array of 10 pointers to int.
int ptr[10]; declares an array of 10 integers (not a pointer).
How to use it: if ptr points to an array, access elements with (*ptr)[i]. For example, (*ptr)[0] is the first int in the array pointed to by ptr.
Pointer arithmetic: ptr + 1 advances by the size of the entire int[10] array (skipping past 10 ints), because ptr points to an array, not to a single int.