Let's take a look at a piece of code.
# Include <stdio. h> <br/> int main (void) <br/>{< br/> int A [5] = {1, 2, 3, 4, 5 }; <br/> int * ptr1 = (int *) (& A + 1); <br/> int * ptr2 = (int *) (INT) A + 1 ); <br/> printf ("% x/N", ptr1 [-1], * ptr2); <br/> return 0; <br/>}
If you are interested, you can guess the output value =. = ~
Answer: "5 2000000"
This involves the meaning of the C array name.
And size-end Storage Structure
First, the first ptr1
It is used to describe the meaning of the C array name.
We know array name.
Which indicates
First address of array
Assume that the address of a is
0x0012ff6c
So ptr1 = (int *) (&)
The ptr1 value is also
0x0012ff6c
But ptr1 = (int *) (& A + 1)
The value of ptr1 is
0x0012ff80
Let's take a look at the memory.
To put it simply, from 0x0012ff6c
Starting with an array int A [5]
The values include 1, 2, 3, 4, and 5.
You can see ~
0x0012ff80
The first address of array a is stored as a [0] 0x0012ff6c.
The ptr1 value is changed
0x0012ff80
Can you understand why?
In fact, array name
There is another meaning
For example
Const int *;
A = (int *) malloc (5 * sizeof (INT ));
This is our int A [5]
Another version
We can see that array name
Another implication is that
Jurisdiction over its own region
& A + 1
It's not a simple 0x12ff6c + 1
Oh
Neither
0x12ff6c + 4 (INT)
Instead, 0x12ff6c + <range of array a>
That is
0x12ff6c + 5 * (INT)-> 0x12ff6c + 0x14 = 0x12ff80
So array name
The region is also given when space is initially allocated.
& A + 1
+ 1 in
Is
The size of the space under the jurisdiction of span array name
So
Ptr1 now points
0x0012ff80
If the output
* Ptr1 is
0x0012ff6c
And the last
PTR [-1] is
0x0012ff80-4 (INT) = 0x0012ff7c
The value stored in 0x0012ff7c is what we get.
5
Bytes ----------------------------------------------------------------------------------------------------
Then the second ptr2
Output is 2000000
Many people may find it strange that they have not stored this value.
Therefore, it is easy to understand the large-end and small-end storage modes.
Let's take a look at the memory and then analyze it.
My CPU is AMD, and we can see that it uses the small-end storage mode.
Ptr2 = (int *) (INT) A + 1)
First (INT)
That is
First address of array
Note that &
And (INT)
The meaning is different, resulting in + 1
The meaning is also different.
(INT)
The value is 0x0012ff6c.
Pointing to a [0]
A [0]
The value is 1.
Memory Distribution is
0x0012ff6a
00 00 01 00 00 00
02 00 00 00 03
Then (INT) A + 1
There is no regional span in the first case
Simply + 1 byte
So (INT) A + 1
Yes
0x0012ff6c + 1 = 0x0012ff6d
Because it is directed to the address, it takes 4 bytes consecutively.
As Value
Ptr2 now points to the memory condition:
0x0012ff6a
00 00 01 00 00 00 02
00 00 00 03
Because it is a small-end Storage Structure
Therefore, the output is naturally 2000000.