For one-dimensional arrays, the behavior of pointers and arrays is very similar.
#include <iostream>voidTestonedim () {intA[] = {1,2,3,4,5}; CharB[] ="abcdef"; int* AP =A; Char* BP =b; Std::cout<<"AP:"<< AP <<Std::endl; Std::cout<<"A:"<< a <<Std::endl; Std::cout<<"BP:"<< BP <<Std::endl; Std::cout<<"B:"<< b <<Std::endl; Std::cout<<"Ap[0]:"<< ap[0] <<Std::endl; Std::cout<<"A[0]:"<< a[0] <<Std::endl; Std::cout<<"Bp[0]:"<< bp[0] <<Std::endl; Std::cout<<"B[0]:"<< b[0] <<Std::endl;}intMain () {Testonedim ();}
The output is:
ap:002ef718a:002ef718bp:abcdefb:abcdefap[01a[01bp[ 0]: ab[0]: a
From the results, the behavior of one-dimensional arrays and pointers is consistent.
Second , for a two-dimensional array, the case and a one-dimensional array is somewhat different, such as two-dimensional array can not be directly assigned to pointers pointer; If you want to point a pointer to a two-dimensional array, you need to assign the array to a pointer to the array, and if you want to assign a two-dimensional array to a pointer, You can then assign the pointer to the pointer on the array pointer. The code is as follows:
#include <iostream>voidTesttwodim ();intMain () {Testtwodim ();}voidTesttwodim () {inta[][3] = {1,2,3,4,5}; int* b[] = {a[0], a[1]}; int(* AP) [3] =A; int* * BP =b; Std::cout<<"AP:"<< AP <<Std::endl; Std::cout<<"Ap[0]:"<< ap[0] <<Std::endl; Std::cout<<"Ap[0][0]:"<< ap[0][0] <<Std::endl; Std::cout<<"----------------"<<Std::endl;
Std::cout << "BP:" << BP << Std::endl;
Std::cout << "bp[0": "<< bp[0" << Std::endl;
Std::cout << "bp[0][0": "<< bp[0][0" << Std::endl;
Std::cout << "----------------" << Std::endl;
Std::cout << "A:" << a << Std::endl;
Std::cout << "a[0": "<< a[0" << Std::endl;
Std::cout << "a[0][0": "<< a[0][0" << Std::endl;
}
The output is:
Ap:0042fa1c
Ap[0]: 0042fa1c
Ap[0][0]: 1
----------------
bp:0042fa0c
Bp[0]: 0042fa1c
Bp[0][0]: 1
----------------
A:0042fa1c
A[0]: 0042fa1c
A[0][0]: 1
From the result, the pointer to the array and the behavior of the two-dimensional array are consistent, the pointer and the two-dimensional array behavior is not exactly the same, but the value is already the same.
Arrays and pointers