Relationship between two-dimensional arrays and pointers in C ++

Source: Internet
Author: User

I learned from clicking open link and clicking open link, and made some modifications.

When I opened C ++ primer again, I couldn't help but lament the rigor and meticulousness of this book. Then we can learn arrays and pointers. This section describes the relationship between two-dimensional arrays and pointers.

Literacy + Review:

Array is a composite data type, which must be defined and initialized.

Definition and initialization: the dimension of the array must be defined by a constant expression with a value greater than or equal to 1. This constant expression can only contain an integer const object initialized by a constant expression. Non-const variables and const variables whose values are not known at the running stage cannot be used to define the dimension of the array. The initialized array does not need to be specified. No array of reference type.

The built-in array type defined by the function in vitro, element Initialization is 0, and body definition is not initialized. When the element type is a class type, the default constructor is initialized. If no default constructor is available, initialization must be displayed, regardless of the position defined by the array. When initializing a character array with a string literal value, note that the string literal value contains an additional null character. Again, read from the right to the left when you understand the pointer declaration statement.

 

// The focus of this article is as follows:

Array name-storage of two-dimensional arrays-Relationship between pointers and two-dimensional arrays-conversion between pointers and Arrays

 

1. Let's talk about the array name. The array name represents the pointer to the first element of the array. For a one-dimensional array, the array name represents the address of the first element. For a two-dimensional array, because its elements are arrays, the array name represents the address of the first row.

For two-dimensional array A, distinguish a and a [0]:

A Represents the first row address. Each element of a row is an array of rows. Therefore, the unit of its pointer movement is "row ". Using a [0] is to treat a two-dimensional array as a one-dimensional array, that is, its element is not a row, but a single array element. Therefore, it points to the first element of the array, and its pointer Movement Unit is "single array element ".

 

2. How to store two-dimensional arrays in memory:

Int A [2] [3] = {1, 2, 3, 4, 5, 6 };

Int M = A [0] [5]; // M = 6

Therefore, the array of C ++ does not have a dimension in the memory. When two-dimensional arrays are stored, they are stored in the order of first and last columns. Consider each row as a whole, that is, a large array element.

 

3. Correspondence between pointers and two-dimensional arrays:

1) Address of the Two-dimensional array element corresponding to the pointer:

Int A [2] [3] = {1, 2, 3, 4, 5, 6 };

Int * P = A [0];

Int M = * (p + 3); // M = 4 that is, if p = A [0] (A is a two-dimensional array ), P + J points to a [0] [J].

2) pointer corresponding to the two-dimensional array element address:

Int A [2] [3] = {1, 2, 3, 4, 5, 6 };

Int * P = A [0];

A [I] [J] = * (p + I * columns + J); or a [I] [J] = P [I * columns + J];

3) ing between two-dimensional array names and two-dimensional array elements:

Two-dimensional array names are used as addresses to represent array elements: Two-dimensional array a represents row elements, and a + I represents the I-th row elements. Therefore, a [I] [J] is represented as: * (a + I) + J)

 

4) Row array pointer:

Int A [2] [3] = {1, 2, 3, 4, 5, 6 };

Int ** P = A; // compilation Error

You can define an array pointer so that a pointer variable has the same properties as a two-dimensional array name. Definition method: int (* P) [4] =;

Differentiate array pointers and pointer Arrays: Add 'to the array pointer and pointer array. The pointer is enclosed in parentheses. An array pointer is a pointer to the address of the first element of an array. It is essentially a pointer and is defined as: int (* ptr_array) [10];. A pointer array is an array whose elements are pointers. It is essentially an array and defined as: int * ptr_array [10];.

 

4. in-depth explanation:

Void function (INT ** array, int width, int height)

Then we define a two-dimensional array INTP [3] [3] = {, 3}, {, 6}, {, 9 }};

When we call a function, namely function (p, 3, 3), the compiler reports an error:

Error c2664: 'function': cannot convert parameter 1 from int [3] [3] 'to int **'

The array name and pointer are different. C ++ supports automatic conversion of arrays to pointer types, but pointers cannot be automatically converted to array types. The conversion starts from the outer layer to the inner layer, and the outer layer is the same.

For the sake of convenience, let's first talk about the relationship: think of the pointer array as an array nested in the outer layer of the pointer, and C ++ supports only sequential conversion from the outer layer to the inner layer, if the outer layer is the same, no conversion is performed. Therefore, array (pointer) and P (address of the first element of the array, that is, pointer of the first element of the array) cannot be converted to the same type. For array pointers and pointer Arrays: A pointer array can be converted into a pointer. the pointer is different from an array pointer. It is represented by code:

Int A [2] [3];

INT (* P) [2] = A; // This is not converted. P is the pointer to the array, and A is also

Int * arr [3];

Int ** q = arr; // arr is a pointer array.

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.