C语言 在函数内查找数组的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17590226/
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
Finding length of array inside a function
提问by makhlaghi
In the program below the length of the array aris correct in main but in tempit shows the length of the pointer to arwhich on my computer is 2 (in units of sizeof(int)).
在下面的程序中,数组的长度ar在 main 中是正确的,但temp它显示了ar我计算机上指向的指针的长度为 2(以 为单位sizeof(int))。
#include <stdio.h>
void temp(int ar[]) // this could also be declared as `int *ar`
{
printf("%d\n", (int) sizeof(ar)/sizeof(int));
}
int main(void)
{
int ar[]={1,2,3};
printf("%d\n", (int) sizeof(ar)/sizeof(int));
temp(ar);
return 0;
}
I wanted to know how I should define the function so the length of the array is read correctly in the function.
我想知道我应该如何定义函数,以便在函数中正确读取数组的长度。
回答by Ingo Leonhardt
There is no 'built-in' 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 seperate argument. Alternatively you could have a special value like 0or -1that indicates the end (like it is \0in strings, which are just char []).
But then of course the 'logical' array size was sizeof(arr)/sizeof(int) - 1
没有“内置”方法来确定函数内部的长度。无论您通过什么arr,sizeof(arr)都将始终返回指针大小。所以最好的方法是将元素的数量作为单独的参数传递。或者,您可以有一个特殊的值,如0或-1表示结束(就像它\0在字符串中一样,只是char [])。但是当然,“逻辑”数组大小是sizeof(arr)/sizeof(int) - 1
回答by weston
Don't use a function, use a macro for this:
不要使用函数,为此使用宏:
//Adapted from K&R, p.135 of edition 2.
#define arrayLength(array) (sizeof((array))/sizeof((array)[0]))
int main(void)
{
int ar[]={1,2,3};
printf("%d\n", arrayLength(ar));
return 0;
}
You still cannot use this macro inside a function like your tempwhere the array is passed as a parameter for the reasons others have mentioned.
temp由于其他人提到的原因,您仍然不能在像您这样将数组作为参数传递的函数中使用此宏。
Alternative if you want to pass one data type around is to define a type that has both an array and capacity:
如果您想传递一种数据类型,另一种方法是定义一种同时具有数组和容量的类型:
typedef struct
{
int *values;
int capacity;
} intArray;
void temp(intArray array)
{
printf("%d\n", array.capacity);
}
int main(void)
{
int ar[]= {1, 2, 3};
intArray arr;
arr.values = ar;
arr.capacity = arrayLength(ar);
temp(arr);
return 0;
}
This takes longer to set up, but is useful if you find your self passing it around many many functions.
这需要更长的时间来设置,但如果您发现自己将它传递给许多函数,则很有用。
回答by David Ranieri
As others have said the obvious solution is to pass the length of array as parameter, also you can store this value at the begin of array
正如其他人所说,显而易见的解决方案是将数组的长度作为参数传递,您也可以将此值存储在数组的开头
#include <stdio.h>
void temp(int *ar)
{
printf("%d\n", ar[-1]);
}
int main(void)
{
int ar[]= {0, 1, 2, 3};
ar[0] = sizeof(ar) / sizeof(ar[0]) - 1;
printf("%d\n", ar[0]);
temp(ar + 1);
return 0;
}
回答by Maroun
When you write size(ar)then you're passing a pointerand not an array.
当你写的size(ar)时候,你传递的是一个指针而不是一个数组。
The size of a pointer and an int is 4 or 8 - depending on ABI(Or, as @H2CO3 mentioned - something completely different), so you're getting sizeof(int *)/sizeof int(4/4=1 for 32-bit machines and 8/4=2 for 64-bit machines), which is 1 or 2 (Or.. something different).
指针和 int 的大小是 4 或 8 - 取决于ABI(或者,正如@H2CO3 提到的 - 完全不同的东西),所以你得到sizeof(int *)/sizeof int(32 位机器的 4/4=1 和 8/4= 2 表示 64 位机器),即 1 或 2(或.. 不同的东西)。
Remember, in C when pass an array as an argument to a function, you're passing a pointer to an array.
If you want to pass the size of the array, you should pass it as a separated argument.
请记住,在 C 中,当将数组作为参数传递给函数时,您传递的是指向数组的指针。
如果你想传递数组的大小,你应该将它作为一个单独的参数传递。
回答by Greg Buchholz
You need to wrap the array up into a struct:
您需要将数组包装成一个结构:
#include<stdio.h>
struct foo {int arr[5];};
struct bar {double arr[10];};
void temp(struct foo f, struct bar g)
{
printf("%d\n",(sizeof f.arr)/(sizeof f.arr[0]));
printf("%d\n",(sizeof g.arr)/(sizeof g.arr[0]));
}
void main(void)
{
struct foo tmp1 = {{1,2,3,4,5}};
struct bar tmp2;
temp(tmp1,tmp2);
return;
}
回答by Greg Buchholz
I don't think you could do this using a function. It will always return length of the pointer rather than the length of the whole array.
我不认为你可以使用函数来做到这一点。它将始终返回指针的长度而不是整个数组的长度。
回答by ojblass
Inside the function ar is a pointer so the sizeof operator will return the length of a pointer. The only way to compute it is to make ar global and or change its name. The easiest way to determine the length is size(array_name)/(size_of(int). The other thing you can do is pass this computation into the function.
函数 ar 内部是一个指针,因此 sizeof 运算符将返回指针的长度。计算它的唯一方法是将 ar 设为全局或更改其名称。确定长度的最简单方法是 size(array_name)/(size_of(int)。您可以做的另一件事是将此计算传递给函数。

