Array name, when used to access array elements, it represents the first address of the array, and when you use it to access the array itself, it is the address of the array. It's just that the first address of the array is the same as the address of the array.
a means &a[0], a+1 means &a[1]... At this level, the array name is equivalent to a pointer that can access the internal elements of the array.
&a is the address of the "object" of the array, and &a + 1 represents another array with the same size as the array a. At this level, &a is equivalent to a pointer to access the array as a whole - a pointer to the array.
C-style arrays are flat on the stack and have no other members except the array elements. So, isn’t it normal that the first element of the array is the same as the array address? Or what to call the "first" element.
There is a method that may help you understand Execute the following code
int a[64];
printf("%x %x %x %x", a, a+1, &a, (&a)+1);
On my machine, the running results are: 29fe10 29fe14 29fe10 29ff10 The difference between the first result and the second result is 4, and the difference between the third result and the fourth result is 0x100 4 It is exactly the length of int, 0x100 (256) is exactly the length of an int array with a length of 64
a is the address of the first element of this array, so you can access the i-th element using *(a+i), and &a is the address of this array, but because the address of a piece of data is its first word section address, so their values are the same
In C language, when the array name is used as a function parameter, it will be converted into a pointer pointing to the first element of the array , and because the address of the first element of the array is equal to the array address , so the a and &a parameters passed to printf here, the values corresponding to their objects inside the function are equal, and both are equal to the address of the array.
The values of a and &a are both addresses, and the two addresses are equal.
But the types of a and &a are different. Specifically: The type of a is int * The type of &a is int (*)[2]
This type will affect the results of pointer addition and subtraction operations, so the results of a+1 and (&a)+1 are different. In this example, a+1 is 4 bytes more than a, and (&a)+1 is 8 bytes more than &a
Array name, when used to access array elements, it represents the first address of the array, and when you use it to access the array itself, it is the address of the array. It's just that the first address of the array is the same as the address of the array.
a
means&a[0]
,a+1
means&a[1]
... At this level, the array name is equivalent to a pointer that can access the internal elements of the array.&a
is the address of the "object" of the array, and&a + 1
represents another array with the same size as the arraya
. At this level,&a
is equivalent to a pointer to access the array as a whole - a pointer to the array.C-style arrays are flat on the stack and have no other members except the array elements. So, isn’t it normal that the first element of the array is the same as the array address? Or what to call the "first" element.
There is a method that may help you understand
Execute the following code
On my machine, the running results are:
29fe10 29fe14 29fe10 29ff10
The difference between the first result and the second result is 4, and the difference between the third result and the fourth result is 0x100
4 It is exactly the length of int, 0x100 (256) is exactly the length of an int array with a length of 64
a is the address of the first element of this array, so you can access the i-th element using *(a+i), and &a is the address of this array, but because the address of a piece of data is its first word section address, so their values are the same
These five values are exactly the same
If a variable stores its own address, then taking the address 10,000 times is also its own address,
The above is incorrect.
In C language, when the array name is used as a function parameter, it will be converted into a pointer pointing to the first element of the array , and because the address of the first element of the array is equal to the array address , so the
a
and&a
parameters passed to printf here, the values corresponding to their objects inside the function are equal, and both are equal to the address of the array.&a
is the address of the arraya
is the first address of the first element of the array&a + 1
has gone outside the arraya + 1
is the address of the second element of the arrayThe values of a and &a are both addresses, and the two addresses are equal.
But the types of a and &a are different. Specifically:
The type of a is int *
The type of &a is int (*)[2]
This type will affect the results of pointer addition and subtraction operations, so the results of a+1 and (&a)+1 are different.
In this example, a+1 is 4 bytes more than a, and (&a)+1 is 8 bytes more than &a
In C, the value of the array name is a pointer constant, which is the address of the first element of the array
So
&a, a, &a[0] are equivalent