[英]How can i find the size of a dynamically allocated array in C?
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?我该如何修复它以便它与我的指针一起使用?
I assume you are allocating the array using one of new or malloc/calloc.我假设您正在使用 new 或 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.
您需要跟踪另一个变量中的大小,或者使用一个可以为您跟踪大小的结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.