Create two-dimensional arrays and two-dimensional arrays
Method 1: Use array pointers to open up space with new
<span style="font-size:18px;">#include<iostream>using namespace std;#define ROW 3#define COL 4void main(){int(*p)[COL] = new int[ROW][COL];for(int i=0; i<ROW; ++i){for(int j=0; j<COL; ++j){p[i][j] = i+j;}}for(i=0; i<ROW; ++i){for(int j=0; j<COL; ++j){cout<<p[i][j]<<" ";}cout<<endl;}delete []p;}</span>
Method 2: Use the C language method, for example:
The Code is as follows:
#include <iostream>using namespace std;#define ROW 3#define COL 4void main(){int **p = (int **)malloc(sizeof(int*)*ROW);for(int i=0; i<ROW; ++i){p[i] = (int *)malloc(sizeof(int) * COL);}for(i=0; i<ROW; ++i){for(int j=0; j<COL; ++j){p[i][j] = i+j;}}for(i=0; i<ROW; ++i){for(int j=0; j<COL; ++j){cout<<p[i][j]<<" ";}cout<<endl;}for(i=0; i<ROW; ++i){free(p[i]); //delete []p[i]}free(p); //delete []p;}
I hope you can point out that the code is not comprehensive ~ Thank you ~