C Declaration Syntax: Arrays of Pointers vs Pointers to Arrays
BehavioralModel answerLast updated
Distinguish between an array of pointers and a pointer to an array in C, explaining their memory layouts and pointer arithmetic behavior.
Understanding C Declaration Syntax
C's declaration syntax can be confusing because of operator precedence rules. Consider these two declarations:
int *arr[5];
int (*ptr)[5];
Your task is to explain the fundamental differences between these constructs.
What You Should Cover
-
Type Analysis: Describe what
arrandptractually represent. What types do they have? -
Memory Layout and Arithmetic: Show how to initialize both constructs with example data. When you compute
arr + 1versusptr + 1, what address offset occurs in each case? Explain the underlying reason for the difference. -
Function Parameters: Compare the behavior when these types are passed to functions. How does
void func(int *arr[])differ fromvoid func(int (*ptr)[5])? What information is preserved or lost? -
Code Example: Provide working C code that demonstrates initialization, element access, and iteration for both declarations.
Assumptions
- Target platform is 64-bit where pointers are 8 bytes and
sizeof(int)is 4 bytes. - Emphasize the memory organization and how pointer arithmetic works for each type.
Expected Depth
Your answer should include concrete examples, explain the declaration reading rules, show the memory layout differences, and discuss common mistakes developers make with these constructs.
Hints
Hint 1
Start by parsing each declaration from the identifier outward, remembering that [] binds more tightly than * unless parentheses override precedence.
Hint 2
For pointer arithmetic, remember that adding 1 to a pointer advances by the size of the pointed-to type, not always by one byte.
Hint 3
When arrays are passed as function parameters, they decay to pointers, but pointers to arrays preserve their dimension information.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer: C Declaration Syntax
Type Interpretation
The key to parsing C declarations is reading from the identifier outward, with postfix operators (like []) binding more tightly than prefix operators (like *).
int *arr[5];
- Read as:
arris an array of 5 elements, where each element is a pointer toint - Type:
int *[5](array of 5 pointers) - Size:
5 × 8 = 40bytes (five pointer slots)
int (*ptr)[5];
- Read as:
ptris a pointer to an array of 5intvalues - Type:
int (*)[5](pointer to array) - Size:
8bytes (single pointer)
The parentheses in the second declaration override the default precedence, making ptr a pointer first, then specifying what it points to (an array of 5 integers).
Memory Layout
These types organize memory differently:
int *arr[5]:
[slot0][slot1][slot2][slot3][slot4] ← 40 bytes of pointer storage
| | | | |
↓ ↓ ↓ ↓ ↓
separate int values anywhere in memory
int (*ptr)[5]:
[ptr: 8 bytes] ──→ [i₀][i₁][i₂][i₃][i₄] ← contiguous 20-byte block
With arr, you have five independent pointers that can reference integers located anywhere. With ptr, you have a single pointer that references a contiguous sequence of five integers.
Pointer Arithmetic
Incrementing a pointer always advances by the size of its pointed-to type:
-
arr + 1: In expressions,arrdecays to a pointer to its first element (typeint **). Adding 1 moves forward bysizeof(int *)= 8 bytes, landing at&arr[1]. -
ptr + 1: Hereptrpoints toint[5], so adding 1 advances bysizeof(int[5])= 20 bytes, moving to the next complete row of five integers.
This difference is critical: arr + 1 steps through pointer slots, while ptr + 1 steps through entire integer arrays.
Code Demonstration
#include <stdio.h>
#include <stddef.h>
void process_pointer_array(int *a[], size_t count) {
// Parameter adjusted to int **, dimension lost
printf("In function: sizeof(a) = %zu\n", sizeof(a)); // 8 bytes
for (size_t i = 0; i < count; i++) {
printf("%d ", *a[i]);
}
printf("\n");
}
void process_array_pointer(int (*p)[5], size_t rows) {
// Dimension preserved: p knows each row has 5 elements
printf("In function: sizeof(*p) = %zu\n", sizeof(*p)); // 20 bytes
for (size_t r = 0; r < rows; r++) {
for (size_t c = 0; c < 5; c++) {
printf("%d ", p[r][c]);
}
printf("\n");
}
}
int main(void) {
// Example 1: Array of pointers
int x = 100, y = 200, z = 300, w = 400, v = 500;
int *arr[5] = {&x, &y, &z, &w, &v};
// Example 2: Pointer to array
int data[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
int (*ptr)[5] = data;
printf("sizeof(arr) = %zu, sizeof(ptr) = %zu\n", sizeof(arr), sizeof(ptr));
// Output: 40, 8
// Element access
printf("*arr[1] = %d\n", *arr[1]); // 200
printf("(*ptr)[2] = %d\n", (*ptr)[2]); // 3
// Pointer arithmetic offsets
ptrdiff_t offset1 = (char *)(arr + 1) - (char *)arr;
ptrdiff_t offset2 = (char *)(ptr + 1) - (char *)ptr;
printf("arr+1 moves %td bytes, ptr+1 moves %td bytes\n", offset1, offset2);
// Output: 8, 20
// Iteration: arr is not modifiable, use a copy
int **p = arr;
p++; // now points to &arr[1]
printf("After increment: **p = %d\n", **p); // 200
// ptr can be incremented directly
ptr++; // now points to &data[1]
printf("After increment: (*ptr)[0] = %d\n", (*ptr)[0]); // 6
// Function calls
ptr = data; // reset
process_pointer_array(arr, 5);
process_array_pointer(ptr, 2);
return 0;
}
Function Parameter Behavior
void func(int *arr[]):
- Adjusted to
int **arrby parameter decay rules - The dimension is completely lost
sizeof(arr)inside the function is 8 bytes (pointer size)- You must pass the count separately
- Works with jagged arrays where each pointer references different-sized data
void func(int (*ptr)[5]):
- Equivalent to
int ptr[][5] - The column dimension (5) is part of the type signature
sizeof(*ptr)gives 20 bytes (one complete row)- Indexing like
ptr[r][c]is computed asbase + r×20 + c×4 - Type-checked: passing
int[3][6]causes a compiler error
Critical mistake: A 2D array like int matrix[3][5] does not decay to int **. It decays only one level to int (*)[5], matching the second function signature but not the first.
Common Pitfalls
-
Indexing confusion:
*ptr[i]means*(ptr[i])(dereference elementi), while(*ptr)[i]means dereferenceptrfirst, then index. Without parentheses on a pointer-to-array, you get the wrong element. -
Type mismatches: Passing
arrto a function expectingint (*)[5]fails—the types are incompatible and memory layouts differ. -
Out-of-bounds arithmetic: Pointer arithmetic beyond array bounds (except one-past-the-end) is undefined behavior.
-
Uninitialized pointers:
int *arr[5];at block scope contains garbage. Initialize explicitly.
Complexity Analysis
All operations (decay, increment, indexing) are O(1) time with no extra space. Storage differs: arr uses 40 bytes plus the memory for target integers; ptr uses 8 bytes plus a contiguous block.
Trade-offs
Array of pointers (int *arr[5]):
- Flexibility: can point to non-contiguous memory
- Overhead: extra indirection and pointer storage
- Use case: sparse data, rows of varying length
Pointer to array (int (*ptr)[5]):
- Efficiency: single indirection, cache-friendly
- Constraint: requires contiguous layout
- Use case: dense matrices, multi-dimensional numerical data
Understanding these distinctions is essential for systems programming, embedded development, and working with legacy C code that uses complex declaration syntax.