// Justin C. Miller
// created on 10-2-2001
// created for http://www.geocities.com/neonprimetime.geo/index.html
// 2D ARRAY AND MALLOC AGAIN
#include
using namespace std ;
int main(){
int i ;
int nrows = 3 ;
int ncolumns = 2 ;
cout << "I want an array with " << nrows << " rows and " << ncolumns << " columns." << endl ;
// CREATE 2D ARRAY IN THE HEAP USING MALLOC
int **array = (int **)malloc(nrows * sizeof(int *));
cout << "First, i grabbed space for " << nrows << " rows of int*" << endl ;
// array is a pointer to pointers!!!!!
// malloc grabs space for your rows of pointers
for(i = 0; i < nrows; i++){
array[i] = (int *)malloc(ncolumns * sizeof(int));
cout << "Then I grabbed space for " << ncolumns << " integers in row " << (i+1) << endl ;
}
// malloc now grabs space for integers to fill the columns
// OUTPUT IS
/*
I want an array with 3 rows and 2 columns
First, i grabbed space for 3 rows of int*
Then I grabbed space for 2 integers in row 1
Then I grabbed space for 2 integers in row 2
Then I grabbed space for 2 integers in row 3
*/
return 0 ;
}
               (
geocities.com/neonprimetime.geo/cpp)                   (
geocities.com/neonprimetime.geo)