在指针多维数组的数组大小

编程入门 行业动态 更新时间:2024-10-23 22:24:46
本文介绍了在指针多维数组的数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

在指针多维数组的数组大小

本文发布于:2023-07-29 00:48:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   多维   指针   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!