[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: c++: 2D array
HI,
Here is another way I figured out. The advantage of using this is
that the entire array can be copied with a single memcpy()
here is the code which explains all
<snip>
#include<iostream>
int main()
{
// create the pointers to rows
int ** ptr = new int*[4];
// let the first row point to the entire data
ptr[0] = new int[5 * 4];
// let each subsequent row index into the
// allocated data
for(int i = 1; i < 4; i++)
{
ptr[i] = ptr[i-1] + 5 ;
}
ptr[0][1] = 10;
ptr[3][4] = 10;
cout << ptr[0][0] << endl ;
cout << ptr[0][1] << endl ;
cout << ptr[3][4] << endl ;
}
even though you can index into the array using ptr[i][j], the entire data
is pointed to by ptr[0], so copying the array will be much easier.
Cheers,
Soj.
--
Sojan James
National Semi. India Designs Ltd.
7/6 Brunton Road, Bangalore 560025. India.
Ph: 5587918/919/920 ext. 308