在指针多维数组的数组大小 [英] Array size in multidimensional array with pointers

查看:101
本文介绍了在指针多维数组的数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用,以这种方式创建多维数组库:

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.

这篇关于在指针多维数组的数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆