& A and a difference

When we define an array a, the compiler determines the size (number of elements in the element Type Size *) depending on the type of memory allocation of a specified number of elements and element, and the name of this memory is a. Once this memory with the name of a match can not be changed. a [0], a [1] as the element a, but the name is not the element. Each element of the array is no name.

Here & a [0] & a and in the end what difference does it make? a [0] is an element, a is a whole array, although & a [0] & a same value, but not the same meaning. The former is the first address of the array element, which is the first address of the array. And the access subject accesses a form pointer to remember the number of the offset unit is not the number of byte elements, in the calculation of the new address do not mistake.

Look at the following example:

#include<iostream>

using namespace std;

int main ()

{

int a[5]={1,2,3,4,5};

int* ptr=(int*)(&a+1);

printf("%d,%d",*(a+1),*(ptr-1));

system("pause");

return 0;

}

Answer: 2,5

Resolution:

int * ptr = (int *) (& a + 1); & a is the array a first address of the pointer is incremented, thereby obtaining the address of the next element, not the original address values ​​applied directly to a so & a + 1 & a, compared with the first address plus 5 * sizeof (int), apparently the current pointer has exceeded the bounds of the array. The address calculated in the previous step, cast to type int *, assigned ptr.

* (A + 1); a, & value a is the same, but the meaning is the same. a first address is the first element of the array, i.e., [0] is the first address, a + 1 is the first address of the array element, i.e., a [1], & a + 1 is the first address of the next array. The output 2

* (Ptr-1); ptr points to as A [5], and is of type int * ptr, so * (ptr-1) is a pointer to a [4], the output 5.

 

Here & a [0] & a and in the end what difference does it make? a [0] is an element, a is a whole array, although & a [0] & a same value, but not the same meaning. The former is the first address of the array element, which is the first address of the array. And the access subject accesses a form pointer to remember the number of the offset unit is not the number of byte elements, in the calculation of the new address do not mistake.

Look at the following example:

#include<iostream>

using namespace std;

int main ()

{

int a[5]={1,2,3,4,5};

int* ptr=(int*)(&a+1);

printf("%d,%d",*(a+1),*(ptr-1));

system("pause");

return 0;

}

Answer: 2,5

Resolution:

int * ptr = (int *) (& a + 1); & a is the array a first address of the pointer is incremented, thereby obtaining the address of the next element, not the original address values ​​applied directly to a so & a + 1 & a, compared with the first address plus 5 * sizeof (int), apparently the current pointer has exceeded the bounds of the array. The address calculated in the previous step, cast to type int *, assigned ptr.

* (A + 1); a, & value a is the same, but the meaning is the same. a first address is the first element of the array, i.e., [0] is the first address, a + 1 is the first address of the array element, i.e., a [1], & a + 1 is the first address of the next array. The output 2

* (Ptr-1); ptr points to as A [5], and is of type int * ptr, so * (ptr-1) is a pointer to a [4], the output 5.

 

Guess you like

Origin www.cnblogs.com/linux-bfbdxj520/p/11404636.html