Array pointer, pointer array, and two-digit Array

Source: Internet
Author: User
Document directory
  • More optimized methods
Int * P [3] and INT (* P) [3]

* P [3] is a pointer array, which means that every element in the array is a pointer variable, and (* P) [3], P is a pointer variable that points to a one-dimensional array containing three integer elements.

View code

Int I, j; int A [2] [3] = {3, 4, 5, 6, 7, 8}; // int * P [3]; // represents an array, the elements in the array are pointer types. There are three elements in total: int (* q) [3]; // a pointer, point to an array containing three int types (q + 1) three array elements are skipped. // the address of the three elements in the first row is stored in the p pointer array for (I = 0; I <3; ++ I) P [I] = & A [0] [I]; // The value of the address in the output pointer array for (j = 0; j <3; ++ J) cout <* P [J] <""; // The output result is 3, 4, and 5 cout <Endl; q =; // assign the starting address of array A to the Q; for (I = 0; I <2; I ++) for (j = 0; j <3; j ++) cout <* (q + I) + J) <""; // output the element system ("pause") in the array ");
PS:, see C ++ Primer

Strictly speaking, there is no multi-dimensional array in C ++. Generally, the multi-dimensional array is actually an array, such as int arry [3] [4]; represents an array with a length of 3. Each element in the array is an array with a length of 4. When using multi-dimensional arrays, remember this to understand its application.

The following describes the relationship between multi-dimensional arrays and pointers. Like an ordinary array, when a multi-dimensional array is used, it is actually automatically converted to a pointer pointing to the first element of the array. That is to say, the array name is a pointer to the first element in the array. In a one-dimensional array, arry = & arry [0], the two addresses are the same. In a two-dimensional array, the array name points to the first element, and the first element is an array with a length of 4. We define a pointer int (* P) [4] to an array with a length of 4, and then assign the first address of the Two-dimensional array to it, P = Arry. In this way, values can be assigned. Arry = & arry [0].

Knowing the relationship between two-dimensional array names and pointers, we will understand much better when passing parameters in two-dimensional arrays. Passing parameters in two-dimensional arrays has always been a headache. Here we still pass the two-dimensional array name as a real parameter. In the form parameter that accepts the function, we only need to define a pointer pointing to an array with a specific length, for example, here we use int (* P) [4] to accept parameters such as Arry. The following code example is provided.

View code

# Include <iostream> # include <stdlib. h> using namespace STD; // The array name is a pointer to the first element of the array. Here we define a pointer to the array to accept arry // R to indicate the number of rows in the two arrays, C indicates the number of columns in a two-dimensional array. Void printarry (INT (* arry) [4], int R, int c) {for (INT I = 0; I <r; I ++) {for (Int J = 0; j <C; j ++) {cout <arry [I] [J] <";}cout <Endl ;}} void main () {int arry [3] [4] = {,}, {,}, {,}; printarry (Arry, 3, 4); // equivalent to printarry (& arry [0], 3, 4); System ("pause ");}

The preceding simple example of printing a two-dimensional array focuses on passing parameters of a two-dimensional array.

More optimized methods

In the above example, the form parameter must specify the arry pointer pointing to an array of the maximum length,For example, INT (* arry) [4] must be specified as 4There are some limitations, so is there a better way. The answer is yes. ConsideringTwo-dimensional arrays occupy continuous space in the memoryWe can use an array to express two-digit arrays. Rewrite the printarry method as follows:

View code

# Include <iostream> # include <stdlib. h> using namespace STD; // input the array pointer. The number of rows and columns of the Two-dimensional array void printarry2 (int * Arry, int R, int c) {for (INT I = 0; I <r * C; I ++) {cout <arry [I] <"" ;}cout <Endl ;} void main () {int arry [4] [4] = {1, 2, 8, 9}, {2, 4, 12}, {4, 7, 10, 13}, {6, 8, 11, 15 }}; printarry2 (& arry [0] [0],); // enter the address of the first element in the first array in the array system ("pause ");}


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: 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.