/*
* File Name: Dynamic2DimentionalArray.cpp
* Author: MH
* Since 2013/03/03
* Toolkit: Dev C++ 4.9.9.9
*/
#include <iostream>
using namespace std;
int main(){
int r, c, **p, i, j;
cout << "enter the row: ";
cin >> r;
cout << "enter the column: ";
cin >> c;
cout << endl;
p = new int *[r]; // generating rows
for (i=0; i<r; i++) // generating columns
p[i] = new int[c]; // Not new int *[c]!!
for (i=0; i<r; i++) // accessing: assigning values
for (j=0; j<c; j++)
p[i][j] = i;
for (i=0; i<r; i++){ // accessing: printing values
for (j=0; j<c; j++)
cout << p[i][j] << " ";
cout << endl;
}
for (i=0; i<r; i++) // deallocate the memory
delete []p[i]; // delete columns
delete []p;
return 0;
}
搜尋此網誌
[C/C++][動態二維陣列的使用]
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言