#include <iostream>using namespacestd;#defineM 2#defineN 3intMain () {intA[m][n] = {1,2,3,4,5,4}; cout<<& (a[0]) <<endl;//00dcfa64cout<<& (a[0])+1<<endl;//00dcfa70,offest:12cout<<a<<endl;//00dcfa64,a=& (a[0]),cout<<a+1<<endl;//00dcfa64cout<<& (a[0][0]) <<endl;//00dcfa64cout<<& (a[0][0])+1<<endl;//00dcfa68,offest:4 int* ptr = (int*) (a);//ptr=& (a[0][0])cout<<ptr<<endl;//00dcfa64//for (int i = 0; i < sizeof (a)/sizeof (int); i++)//{ //if ((*ptr++) = = 4)// { //cout << "i=" << i/n << ", j=" << i%n << Endl; // } //} return 0;}
The value of &a[0], &a[0][0] is equal, but the meaning is different (for the compiler), because A[0] holds a pointer to an integer array with 3 elements, and &a[0] is the address of the array, and for a[0][0], a[0 ][0] is the first data in an array of a[0], which is an integer, &a[0][0] is the address of the integer, so the result is different on the pointer operation.
In understanding, the variable A is an array of 2 elements, and the 2 elements are an integer array with 3 elements.
int A[m][n] = {1,2,3,4,5,4}; Write int A[m][n] = {{1,2,3},{4,5,4}}; Easier to understand.
Array name A=&a[0]
In addition, two-dimensional arrays occupy contiguous space in memory, storing rows of elements from top to bottom in memory and storing them in the same row in order from left to right. Therefore, you can consider defining a pointer int* ptr = (int*) (a); To make it easier to access the elements of a two-dimensional array.
#include <iostream>using namespacestd;//in a two-dimensional array matrix, each row is incremented from left to right,//Each column is sorted from top to bottom//determines whether an integer is in the two-dimensional arrayBOOLFind (int* Matrix,intRowsintColumnsintNumber ) { BOOLFound =false; if(Matrix! = NULL && rows >0&& columns >0) { introw =0; intcolumn = Columns-1; while(Row < rows && column >=0) { if(Matrix[row * columns + column] = =Number ) {Found=true; Break; } Else if(Matrix[row * columns + column] >Number )--column; Else++Row; } } returnfound;}
In the above example, when you pass a parameter to the function find (), use the (int*) matrix instead of using the two-dimensional array name matrix directly.
Notice how the elements within the array are indexed.
Two-dimensional array names and pointers