Home > Article > Backend Development > Why is the address of an array in C equivalent to the address of a pointer to its first element?
Understanding the Equivalence of Array Addresses and Converted Pointers
In the C programming language, it is a common practice to convert an array to a pointer to its first element using the array-to-pointer conversion. This raises the question of why the address of the array is the same as the address of the converted pointer.
To illustrate this concept, let's consider the following code:
int t[10]; int *u = t; cout << t << " " << &t << endl; cout << u << " " << &u << endl;
The output of this code demonstrates the equivalence of array addresses and converted pointer addresses:
0045FB88 0045FB88 0045FB88 0045FB7C
To understand this phenomenon, we need to recognize two distinct meanings associated with the symbol t:
In our code, the first line of the output displays the address of t (the array) and the address of &t (the pointer to the array). Since arrays are contiguous blocks of memory, their starting addresses are identical.
The second line of output shows the address of u (the converted pointer to the first element) and the address of &u (the pointer to the converted pointer). Again, these addresses are the same because u is essentially equivalent to &t[0].
Therefore, we conclude that the reason the address of an array is the same as the address of the array converted to a pointer is because both represent the starting address of the contiguous memory block occupied by the array. This equivalence holds because the array-to-pointer conversion produces a pointer to the first element of the array, which has the same memory location as the start of the entire array.
The above is the detailed content of Why is the address of an array in C equivalent to the address of a pointer to its first element?. For more information, please follow other related articles on the PHP Chinese website!