Many beginners cannot figure out the relationship between pointers and arrays. I will tell you: there are no
Any relationship! They often wear similar clothes to tease you.
A pointer is a pointer. A pointer variable occupies 4 bytes in a 32-bit system, and its value is a memory address.
The pointer can point to any place, but not any place you can access it through this pointer variable.
An array is an array. Its size depends on the type and number of elements. You must specify the element type when defining an array.
And number. Arrays can store any type of data, but cannot store functions.
--------------------------------------------------------------------
Below we will discuss in detail some of the plausible characteristics between them. For example, the function is defined as follows:
A), char * P = "abcdef ";
B), char a [] = "123456 ";
1. Access the pointer in the form of a pointer and
Example A) defines a pointer Variable P. P occupies 4 bytes on the stack, and P stores the first part of memory.
Address. There is a static zone in the block, and the size of the space is 7 bytes. The memory has no name. Access to this memory
The question is completely anonymous access. For example, to read the character 'E', we have two methods:
1), in the form of a pointer: * (p + 4 ). Take out the address value stored in P, assume it is 0x0000ff00, and then add
The offset of the last 4 characters to obtain the new address 0x0000ff04. Then, retrieve the value on the 0x0000ff04 address.
2), in the following format: P [4]. The compiler always resolves the following operations in the form of a pointer.
. P [4] this operation will be parsed into: First extract the address value stored in P, and then add the bias of the four elements in brackets
Calculate the new address, and then retrieve the value from the new address. That is to say, access in the following form is essentially
There is no difference with access in the form of pointers, but it is just different in writing.
2. Access the array as a pointer and as a pointer
Example B) defines an array A. A has seven char elements whose space is 7. Array a itself
On the stack. To access element A, you must first find the first address of the first element of the array according to array name A. Then
Offset to find the corresponding value. This is a typical "name + anonymous" access. For example, to read the '5' character ',
We have two methods:
1), in the form of a pointer: * (a + 4 ). A Represents the first address of the array's first element. Assume It is 0x0000ff00,
Add the offset of 4 characters to get the new address 0x0000ff04. Then retrieve
Value.
2), in the following format: A [4]. The compiler always resolves the following operations in the form of a pointer.
. A [4] this operation will be parsed into: A as the first address of the array's first element, and then add the four elements in brackets
Offset, calculate the new address, and then retrieve the value from the new address.
From the above analysis, we can see that pointers and arrays are totally different. Only they
Can be accessed in Pointer form or in the following format. One is completely anonymous access, and the other is typical
. Note that this "access in the form of XXX" is an expression.
Another thing to emphasize is that the offset 4 mentioned above represents four elements rather than four bytes. Only
However, the size of the char data is exactly one byte. Remember that the unit of this offset is RMB.
When calculating the new address, do not make a mistake.
Pointer and array learning Summary