In learning the C language, pointers and arrays are essential topics, and the concepts of “pointer arrays” and “array pointers” are particularly confusing for many beginners. These two concepts are like “twins”—they differ by just one word in their names, yet their meanings are vastly different. Today, we will clarify these concepts so that you will no longer confuse them!
First, Understand the Essence: Who is the “Array”, Who is the “Pointer”?
The key to distinguishing between the two lies in understanding their core identities:
•Pointer Array: Essentially an array, where each element is a pointer
•Array Pointer: Essentially a pointer, which specifically points to an array
1. Pointer Array: An Array Containing Pointers
Definition
|
type *array_name[number_of_elements]; |
For example, int *p[5]; means: This is an array (named p) containing 5 elements, each of which is a pointer of type int* (a pointer to an int).
Why is it an array? Because the priority of [] is higher than that of *, so p first combines with [] to form an array, which is then modified by * to indicate that the array elements are pointers.
Memory Layout
Assuming a 64-bit system (where pointers occupy 8 bytes), int *p[5] occupies a contiguous block of memory, totaling 5×8=40 bytes, with each position storing an int type address.
Classic Usage: Handling an Array of Strings
The most common scenario for a pointer array is to store the addresses of multiple strings, which is more flexible than a two-dimensional character array:
|
#include <stdio.h> int main() { // Pointer array: Each element points to a string char *strs[] = { “C Language“, “Pointer Array“, “Array Pointer“, “Hello“ };
// Iterate through the pointer array for (int i = 0; i < 4; i++) { printf(“strs[%d] = %s\n”, i, strs[i]); } return 0; } |
Here, strs is a pointer array, where each element points to the address of a constant string, eliminating the need to predefine a fixed length, which saves more space compared to char strs[4][20].
2. Array Pointer: A Pointer to an Array
Definition
|
type (*pointer_name)[array_length]; |
For example, int (*p)[5]; means: This is a pointer (named p) that points to an “array containing 5 int elements”.
Why is it a pointer? Because the () changes the priority, so p first combines with * to form a pointer, which then points to an array of length 5 of int.
Key Feature: Pointer Step Size
An array pointer points to the entire array, so when it performs a +1 operation, the address offset is the “size of the entire array”.
For example, int (*p)[5];, if p currently points to address 0x1000, then p+1 points to 0x1014 (assuming int occupies 4 bytes, 5×4=20 bytes, 0x1000+20=0x1014).
Classic Usage: Operating on Two-Dimensional Arrays
Array pointers are commonly used to pass two-dimensional arrays to functions or to iterate through two-dimensional arrays:
|
#include <stdio.h> // Receive a two-dimensional array with an array pointer void printArray(int (*arr)[3], int row) { for (int i = 0; i < row; i++) { for (int j = 0; j < 3; j++) { // arr[i] is the array of the ith row, arr[i][j] is the element in the ith row and jth column printf(“%d “, arr[i][j]); } printf(“\n”); } } int main() { int nums[2][3] = {{1,2,3}, {4,5,6}}; // Array pointer p points to the first row of the two-dimensional array (the entire first row array) int (*p)[3] = nums;
printArray(p, 2); // Pass the array pointer return 0; } |
Core Comparison Table of Differences
|
Feature |
Pointer Array (e.g., int *p[5]) |
Array Pointer (e.g., int (*p)[5]) |
|
Essence |
Array (elements are pointers) |
Pointer (points to an array) |
|
Priority |
First combines with [] (array) |
First combines with * (pointer) |
|
sizeof Result |
Total size of the array (number of elements × size of pointer) |
Size of the pointer itself (usually 8 bytes) |
|
Typical Usage |
Storing addresses of strings, multiple data addresses |
Operating on two-dimensional arrays, passing the entire array |
Memory Tips
1.Look at the name:“Pointer Array” focuses on “Array”, which is an array containing pointers; “Array Pointer” focuses on “Pointer”, which is a pointer to an array.
2.Look at the parentheses: If there are (), it is an array pointer ((*p)[n]), if not, it is a pointer array (*p[n]).
3.Ask yourself:“Is it an array?”— Yes → Pointer Array;“Is it a pointer?”— Yes → Array Pointer.
Conclusion
The confusion between pointer arrays and array pointers stems from the understanding of “priority” and “essence”. Remember:First determine the core identity (array or pointer), then look at the modification relationship, and you will easily distinguish between them.
In practical development, pointer arrays are suitable for storing multiple same-type pointers (like a collection of strings), while array pointers are suitable for handling two-dimensional arrays or scenarios where the entire array needs to be passed. Practice coding more, and you will quickly master it!
Finally, here’s a little question: int *(*p)[5]; What is it? Let me know the answer in the comments!~
(Note: Some content of this document was generated by AI.)