我如何才能找到一个在C动态分配的数组的大小? [英] How can i find the size of a dynamically allocated array in C?

查看:202
本文介绍了我如何才能找到一个在C动态分配的数组的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了动态通过循环分配的数组。然后,读取数字出阵列的而是一个周期i需要知道数组的大小。该阵列是正确的,全面的工作,并在有正确的价值观。
我定义的阵列是这样的:

I've made an array that is dynamically allocated by a cycle. And then a cycle that reads the numbers out of the array but i need to know the size of the array. The array is correct and fully working and has correct values in it. I defined the array like this:

int *array;

现在,当我想,因为使用指针即时通讯使用这个它不会工作:

Now when i want to use this it wont work because im using a pointer:

int size = sizeof(array)/sizeof(array[0]);

如何解决它,它与我的指针的作品?

How can i fix it so it works with my pointer?

推荐答案

我假设你正在使用分配新的或的malloc /释放calloc的一个数组。在这种情况下,你不能做到这一点。您需要跟踪的大小,另一个变量或使用,将跟踪的大小为你的结构。

I assume you are allocating the array using one of new or malloc/calloc. In that case, you can't do this. You need to track the size in another variable or use a structure that will track the size for you.

这篇关于我如何才能找到一个在C动态分配的数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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