INT * P [2] Different from int (* p) [2]

zhaozj2021-02-16  53

(1) INT * P [2] is a pointer array pointing to the INT type, namely: P is a pointer array containing two elements, and the pointer points to an INT type.

It can be used in this way:

#include

Void main () {

INT * P [2]; int A [3] = {1, 2, 3}; int B [4] = {4, 5, 6, 7}; p [0] = a; p [1] = B ; For (int i = 0; i <3; i ) cout << * p [0] i; // cout << ** p i; cout << endl; for (i = 0; i <4 ; i ) cout << * p [1] i; // cout << ** P i;}

(2) For int (* p) [2], it is equivalent to a two-dimensional array of usage, just it is an array of N rows 2, which can be used:

#include

Void main () {

INT (* P) ​​[2]; INT B [3] [2] = {{1, 2}, {3, 4}, {5, 6}}; p = b; for (int i = 0; i <3; i ) {for (int J = 0; j <2; j ) // cout << p [i] [j]; // cout << * (* (p i) j); cout << endl;}}

Note: For (1) is determined for the number of rows, the number of columns is uncertain, that is, 2 * n type. (2) The pointer usage of the N * 2 array, that is, the number of lines is uncertain, the column number is determined. For (1) equivalent forms of equivalents are as follows:

#include

Void main () {

Int ** array; array = new int * [[3]; int A [3] = {1, 2, 3}; int B [4] = {4, 5, 6, 7}; array [0] = a ; // * array = a; array [1] = b; // * (array 1) = b; for (int i = 0; i <3; i ) cout << array [0] [i]; // cout << * array [0] i; cout << endl; for (int J = 0; j <4; j ) cout << array [1] [j]; // cout << * array [ 1] J;}

In fact, the above usage is the usage of this dynamic two-dimensional array of dynamic two-dimensional arrays.

转载请注明原文地址:https://www.9cbs.com/read-23091.html

New Post(0)