Graphical multi-level pointers and "multidimensional" arrays

Source: Internet
Author: User

Pointers and arrays are very important elements in C/s + + programming and are also more difficult to understand. Among them, multi-level pointers and "multidimensional" arrays are so many people foggy, in fact, as long as the grasp of certain methods, understanding the multi-level pointer and "multidimensional" array can be as simple as understanding the first-level pointer and a one-dimensional array.

First of all, to declare some common sense, if you do not understand the common sense, then you first to make up the basics:

1, in fact, there is no multidimensional array, so-called multidimensional arrays are essentially modeled with one-dimensional arrays.

2. The array name is a constant (meaning that it is not allowed to be assigned), which represents the first address of the first element of the array.

3, the relationship between the array and the pointer is because the array subscript operator [], for example, int a[3][2] is equivalent to * (* (a+3) +2).

4, the pointer is a variable, also has the type, it occupies the memory space size and the system, general 32-bit system, sizeof (pointer variable) = 4.

5. The pointer can add and subtract arithmetic operations, and the basic unit of the addition and subtraction is sizeof (the data type the pointer points to).

6. Array names of arrays are taken to address (&) operation, whose type is the entire array type.

7, the array name of the arrays is the sizeof operator operation, whose value is the size of the entire array in bytes.

8, the array as a function parameter will degenerate into a pointer.

One or one-D arrays:

If there is one-dimensional array as follows:

Char a[3];

The array has a total of 3 elements, the type of the element is char, if you want to define a pointer to the array, that is, if you want to assign the array name A to a pointer variable, then the type of the pointer variable should be what? As previously mentioned, an array name of an array represents the first address of its first element, which is equivalent to &a[0], whereas the type of a[0] is char, so the &a[0] type is char *, so you can define a pointer variable as follows:

char * p = a;//equivalent to char * p = &a[0]

The above text can be represented by the following memory model diagram.

As you should know, a and &a[0] represent the first address of the first element of the array, and if you print the value of &a, you will find that the value is also equal to the first address of the first element of the array . Notice my wording here, that is, &a although the value is also equal to the first element of the array first address, but its type is not the first element of the array initial address type, that is, char *p = &a is wrong.

The previous 6th common sense has said that the array name to take the address operation, whose type is the entire arrays, so the type of &a is char (*) [3], so the correct assignment is as follows:

char (*P) [3] = &a;

Note: Many people like a+1,&a+1,&a[0]+1,sizeof (a), sizeof (&A) to get confused, in fact, as long as the type of the pointer can be solved. For example, in the face of the difference between a+1 and &a+1, because a represents the first element of the array header address, its type is char *, so a+1 is equivalent to the array first address value +sizeof (char), and the type of &a is char (*) [3], representing the entire array, so & The a+1 corresponds to the array's first address value +sizeof (a). (sizeof (a) represents the entire array size, as described in article 7th above)

Two or two-D arrays

If you have the following two-dimensional array:

Char a[3][2];

Since there is actually no multidimensional array, a[3][2] can be thought of as a one-dimensional array with 3 elements, except that the three elements are one-dimensional arrays, respectively. In fact, in memory, the array is actually stored in the context of a one-dimensional array, stored in the order (low address in front): a[0][0], a[0][1], a[1][0], a[1][1], a[2][0], a[2][1].

For the sake of understanding, I have drawn a logical memory diagram, which is logically because the diagram is simply understandable, not the actual storage model of the array in memory (the actual model is described earlier).

As shown, we can divide the array into two dimensions, first the first dimension, a[3][2] as a one-dimensional array with three elements, the elements are: a[0], a[1], a[2], wherein, a[0], a[1], a[2] is a one-dimensional array with two elements ( The element type is char). From the second dimension, here you can think of a[0], a[1], a[2] as the array name that represents the "second dimension" array , and a[0] For example, the one-dimensional array represented by a[0] (array name) is an array of elements with two char types, and a[0] is the array name of this array (representing the first address of the array's first element), so the a[0] type is char *, and the same a[1] and a[2] types are char *. And a is the array name of the first-dimensional array, representing the first-element first address, and the first element is a one-dimensional array with two char-type elements, so a is a pointer to an array of two char-type elements, which is char (*) [2].

In other words, the following assignment is correct:

char (*P) [2] pp = a;//a is the array name of the first-dimensional array, type char (*) [2]

char * p = a[0];//a[0] The array name of the second-dimensional array, type char *

Similarly, the A take address operation represents the address of the entire array, the type is char (*) [3][2], so the following assignment is correct:

char (*P) [3][2] = &a;

Three or three-d arrays

Suppose there are three-dimensional arrays:

Char a[3][2][2];

Similarly, for the sake of understanding, the following logical memory diagram has been deliberately drawn. Because the three-dimensional array is understood in the same way as the two-dimensional array, the detailed analysis is not described in detail.

Four: Multi-level pointers

After dinner and then write ...

Graphical multi-level pointers and "multidimensional" arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.