Thursday, July 9, 2009

2d array in c++?

1/ how can I declare 2d array in c++? Is this correct?





########### CODE ###########


int **array;


array = new int * [rowsize] [columnsize];


................................


delete [ ][ ] array;


########### CODE ###########





2/ How can I get the rowsize and columnsize of a specified 2d array?

2d array in c++?
int a[2][3];


for(int i=0;i%26lt;2;i++)


{


for(int j=0;j%26lt;3;J++)


{


cin%26gt;%26gt;a[i][j];


}


}
Reply:Both decelerations are correct. All the following declarations are exactly same effect.





int **a;


int *a[];


int a[][];





Your other question is bit difficult. There is no way to do that. In C++/C





int a[5][10] and


int a[50]





are interchangeable. i.e. I can declare a[50] and access as a[3][4] and it will work same. You can use ("malloc" to allocate memory not defined through a[50]) "free" and pointer to release memory without knowing the size, But if you need the index for use in any other function then you will have to create variables to hold dimension.


No comments:

Post a Comment