๐งฑ C Pointer to Array โ Accessing Entire Arrays via Pointers in C
๐งฒ Introduction โ What Is a Pointer to an Array in C?
In C programming, a pointer to an array is a pointer that points to the entire arrayโnot just its first element. Unlike a regular pointer (e.g., int *p), which points to a single element, a pointer to an array (e.g., int (*p)[5]) provides access to the whole array block as a unit.
๐ฏ In this guide, youโll learn:
- The syntax of a pointer to an array
- How it’s different from a pointer to the first element
- Examples using both 1D and 2D arrays
- Best practices and practical use cases
๐ Core Concept โ Pointer to an Entire Array
When you use int (*ptr)[5];, you’re declaring ptr as a pointer to an array of 5 integers.
โ Syntax:
int arr[5];
int (*ptr)[5] = &arr;
ptrholds the address of the whole array block*ptrdereferences to the array(*ptr)[i]accesses individual elements
๐ป Code Examples โ Pointer to Array in Action
โ Example 1: Accessing Elements via Array Pointer
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int (*ptr)[5] = &arr;
for (int i = 0; i < 5; i++) {
printf("%d ", (*ptr)[i]);
}
return 0;
}
๐จ๏ธ Output:
10 20 30 40 50
๐ (*ptr)[i] accesses elements from the array that ptr points to.
โ Example 2: Pointer to 2D Array
#include <stdio.h>
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = matrix; // Points to row arrays of size 3
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", p[i][j]);
}
printf("\n");
}
return 0;
}
๐จ๏ธ Output:
1 2 3
4 5 6
๐ p treats matrix as an array of arrays (rows), each of 3 integers.
๐งฉ Pointer to Array vs Array Name vs Pointer to Element
| Expression | Meaning |
|---|---|
int *p = arr | Pointer to first element (arr[0]) |
int (*p)[5] | Pointer to the entire array of 5 int |
arr | Decays to address of arr[0] |
&arr | Address of the whole array |
๐ก Best Practices & Tips
๐ Best Practice:
- Use pointer-to-array when you want to pass an entire array to a function with size context.
๐ก Tip:
- Prefer
int (*p)[N]overint *pfor multi-dimensional arrays or when element grouping matters.
โ ๏ธ Pitfall:
*p[i]is not the same as(*p)[i]. Always use parentheses to correctly dereference array pointers.
๐ ๏ธ Real-World Applications
- ๐ Multi-dimensional matrix processing
- ๐ฆ Passing large arrays to functions safely
- ๐ฎ Game engines (grid maps, tile rendering)
- ๐ง Memory-efficient structure layouts in embedded systems
๐ Summary โ Recap & Next Steps
Pointers to arrays allow working with entire blocks of memory representing arrays. This enhances control over how data is accessed and manipulated, especially in multi-dimensional scenarios.
๐ Key Takeaways:
int (*ptr)[N]is a pointer to an array ofNintegers- Use
(*ptr)[i]to access array elements - Different from
int *pwhich only points to the first element - Vital in 2D array handling and advanced memory management
โ๏ธ Real-World Relevance:
Used in matrix computation, 2D graphics, firmware memory layouts, and high-performance code.
โ Frequently Asked Questions (FAQ)
โ What is the difference between int *p = arr and int (*p)[5] = &arr?
โ
int *p points to the first element, while int (*p)[5] points to the entire array as a unit.
โ How do I pass an entire array to a function using a pointer?
โ
Use int (*p)[N] as a parameter and pass the address: func(&arr);
โ Can I use pointer to array with malloc()?
โ Yes, especially useful for dynamically allocated 2D arrays:
int (*matrix)[cols] = malloc(rows * sizeof(*matrix));
โ Is arr == &arr[0] the same as &arr?
โ No.
arrand&arr[0]both point to the first element.&arrpoints to the entire array block.
โ When should I use a pointer to an array?
โ Use when working with fixed-size blocks, 2D arrays, or when needing size context and tight memory layout.
Share Now :
