How can i find the size of a dynamically allocated array in C?

最后都变了- 提交于 2019-12-10 22:31:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13594935/how-can-i-find-the-size-of-a-dynamically-allocated-array-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!