Tuesday, July 28, 2009

How to know the size of a 2D array in C ?

It's been a while since I've messed with C, but:





arrayName.Length();

How to know the size of a 2D array in C ?
If you have a 2D array as a pointer, you will need to be passed the size explicitly.








Does this help?
Reply:If the 2D array is declared explicitly, like this:


int array[16][8];


Then you can easily tell that the array is 16x8 (if you're looking for bytes, multiply that by sizeof(int)).





If the 2D array is declared as a pointer to a pointer, like this:


int **array;


Then you should find the place where the memory for it is allocated, and check what argument is given to the malloc function. It should look something like:


array = malloc(SIZE*sizeof(int));





I hope this helps.


No comments:

Post a Comment