simple,naive ?
int _tmain(int argc, _TCHAR* argv[]) { //二维指针 int **p = NULL; //行、列 int x,y; cout << "行数:" ; cin >> x; cout << "列数:" ; cin >> y; //申请行空间 p = new(std::nothrow) int*[x]; if( NULL == p) { cout << "memery allocate failed" << endl; return false; } //申请列空间 for(int i = 0; i < x; i++) { p[i] = new(std::nothrow) int[y]; if(NULL == p[i]) { cout << "memery allocate failed" << endl; return false; } } //读入值 int tmp = 1; for(int i=0;i<x;i++) { for(int j=0; j<y; j++) { p[i][j] = tmp; tmp++; } } //输出 for(int i=0;i<x;i++) { for(int j=0; j<y; j++) { cout << p[i][j] << "\t"; } cout << endl; } if(p) { //删除申请的内存 for(int i = 0; i < x; i++) { delete []p[i]; } delete [] p; } system("pause"); return 0; }
输出:
行数:3
列数:4
1 2 3 4
5 6 7 8
9 10 11 12