C语言 查找作为 c 中函数参数接收的整数数组的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25680014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the Size of integer array received as an argument to a function in c
提问by Sunil Hari
i would like to find the size of integer array passed as an argument to a function. Here is my code
我想找到作为参数传递给函数的整数数组的大小。这是我的代码
void getArraySize(int arr[]){
int len = (sizeof(arr)/sizeof(arr[0])
printf("Array Length:%d\n",len);
}
int main(){
int array[] = {1,2,3,4,5};
getArraySize(array);
return 0;
}
I am getting the following warning
我收到以下警告
sizeof on array function parameter will return
size of 'int *' instead of 'int []' [-Wsizeof-array-argument]
sizeof on array function parameter will return
size of 'int *' instead of 'int []' [-Wsizeof-array-argument]
Please help so that i can find the length of integer array inside the functiongetArraySizeHowever am able to find the size of the arrayinside the mainfunction.Since it is passed as a pointer, am not able to find the lengthfrom with in the function.
请帮忙,以便我可以在里面找到整数数组的长度,functiongetArraySize但是我能够找到函数array内部的大小。因为main它是作为 a 传递的pointer,所以我无法length在function.
i do have an idea though.I could put this with in a try/catchblock(Cdoes not have try catch,Only jumperswhich is OS dependent) and loop until i get a segmentation fault.
不过我确实有一个想法。我可以把它放在一个try/catch块中(C没有 try catch,只有依赖于操作系统的跳线)并循环直到我得到一个segmentation fault.
Is there any other way i could use to find the length of integer arrayinside the function
有没有其他方法可以用来找到integer array里面的长度function
回答by paxdiablo
You cannot do it that way. When you pass an array to a function, it decays into a pointer to the first element, at which point knowledge of its size is lost.
你不能那样做。当您将数组传递给函数时,它会衰减为指向第一个元素的指针,此时会丢失对其大小的了解。
If you want to know the size of an array passed to the function, you need to work it out beforedecay and pass that information with the array, something like:
如果您想知道传递给函数的数组的大小,则需要在衰减之前计算出来并将该信息与数组一起传递,例如:
void function (size_t sz, int *arr) { ... }
:
{
int x[20];
function (sizeof(x)/sizeof(*x), x);
}
回答by Jayesh Bhoi
The array decays to a pointer when passed.
数组在传递时衰减为指针。
So sizeof only works to find the length of the array if you apply it to the original array.
因此,如果将 sizeof 应用于原始数组,则它只能用于查找数组的长度。
So you need to pass length of array as separate argument to function.
所以你需要将数组的长度作为单独的参数传递给函数。
回答by barak manos
When you pass an array to a function, its address is passed to the function as a pointer.
当您将数组传递给函数时,其地址将作为指针传递给函数。
So void getArraySize(int arr[])is equivalent to void getArraySize(int* arr).
所以void getArraySize(int arr[])相当于void getArraySize(int* arr).
As a result, sizeof(arr)yields sizeof(int*), which is probably notwhat you want.
结果,sizeof(arr)yields sizeof(int*),这可能不是您想要的。
You can safely use sizeof(arr)/sizeof(arr[0])only within the scope of declaration of arr.
您sizeof(arr)/sizeof(arr[0])只能在声明的范围内安全地使用arr。
In your case, the scope of declaration of arris function main.
在您的情况下,声明的范围arr是 function main。
回答by Sathish
There is no way to determine the length inside the function. However you pass arr, sizeof(arr)will always return the pointer size. So the best way is to pass the number of elements as a separate argument.
没有办法确定函数内部的长度。无论您通过什么arr,sizeof(arr)都将始终返回指针大小。所以最好的方法是将元素的数量作为单独的参数传递。
sizeofonly works to find the length of the array if you apply it to the original array.
sizeof如果将其应用于原始数组,则仅适用于查找数组的长度。
int arr[5]; //real array. NOT a pointer
sizeof(arr); // :)
However, by the time the array decays into a pointer, sizeof will give the size of the pointer and not of the array.
但是,当数组衰减为指针时,sizeof 将给出指针的大小而不是数组的大小。
void getArraySize(int arr[]){
sizeof(arr); // will give the pointer size
}
There is some reasoning as to why this would take place. How could we make things so that a C array also knows its length?
关于为什么会发生这种情况有一些推理。我们怎样才能让 C 数组也知道它的长度呢?
A first idea would be not having arrays decaying into pointers when they are passed to a function and continuing to keep the array length in the type system.
第一个想法是在将数组传递给函数并继续将数组长度保留在类型系统中时,不要让数组衰减为指针。
When you pass an array to a function it decays to pointer. So the sizeoffunction will return the size of int *. This is the warning that your compiler complining about-
当您将数组传递给函数时,它会衰减为指针。因此该sizeof函数将返回 的大小int *。这是您的编译器遵守的警告-
sizeof on array function parameter will return size of 'int *' instead of 'int []'
As i suggested when you pass the array to the function you need to pass the Number of elements also-
正如我所建议的,当您将数组传递给函数时,您还需要传递元素数 -
getArraySize(array, 5); // 5 is the number of elements in array
Catch it by-
抓住它——
void getArraySize(int arr[], int element){
// your stuff
}
Else general way to calculate array size-
计算数组大小的其他一般方法 -
void getArraySize(int arr[], int len){
// your stuff
printf("Array Length:%d\n",len);
}
int main(){
int array[] = {1,2,3,4,5};
int len = (sizeof(arr)/sizeof(arr[0]); // will return the size of array
getArraySize(array, len);
return 0;
}

