我使用,以这种方式创建多维数组库:
I'm using a library that creates a multidimensional array in this way:
const unsigned short arrayA[5] = {1, 2, 3, 4, 5}; const unsigned short arrayB[3] = {7, 8, 9}; const unsigned short *multiArray[] ={arrayA, arrayB};我得到它的价值和它的作品:
I get the values of it and it works:
printf("03: %d\n", multiArray[0][3]); //4 printf("12: %d\n", multiArray[1][2]); //9问题是当我需要得到任何数组的大小,我已经试过这样:
The problem comes when I need to get the size of any of the arrays, I've tried this:
printf("Size of first array: %ld \n", sizeof(multiArray[0])/sizeof(multiArray[0][0]));这是因为它使用地址返回2,很可能。
It returns 2, probably because it's using addresses.
我怎样才能获得数组的大小呢?
How can I get the size of the array then?
我必须这样做,尽量不改变数组声明的方式,所以阵列已经在那里,我只是需要得到的大小。
I have to do this trying not to change the way the arrays are declared, so the arrays are already there and I just need to get the size.
任何方式?
推荐答案这是不可能推断出数组的大小在多阵列指针指向的,因为没有执行任何计算作为两个阵列的方式(arrayA和arrayB)将被存储在任意位置。一个好的做法是将存储数组的大小。
It is not possible to deduce the size of the arrays pointed to by the pointers in multiArray since there is no way of performing any computations as the two arrays (arrayA and arrayB) will be stored in arbitrary locations. A good practice is to store the size of the arrays.
更多推荐
在指针多维数组的数组大小
发布评论