C ++-arrays and pointers

In C ++, the array name is actually a constant pointer to the first element of the array.

As mentioned in the blog about arrays ( C ++-one-dimensional array ), arrays are linear structures in memory. You can imagine there is a badminton tube (that is, the long box containing badminton), assuming that there can be up to five badmintons inside, and the open end is the end. Then you can think of this model as an array, the size of the array is 5 (it can't fit anymore).

Suppose we declare an array:

int list[6] = {11, 12, 13, 14, 15, 16};

Then the way this array is stored in memory is:

 

We can use the pointer to access the value of the array, such as:

 

 C ++ allows pointers to add or subtract an integer, indicating that the address contained by the pointer increases or decreases, and the amount of change is the size of integer * pointer type allocated in memory space

As in the example above, the list array is of type int, so the pointer list is of type int *, because the type of int occupies 4 bits in memory, so list + 1 means that the address increases by 4. That is to say, the address represented by list is 10, and the address represented by list + 1 is 10 + sizeof (int) = 14.

Guess you like

Origin www.cnblogs.com/bwjblogs/p/12699685.html