C语言 c中数组的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7545428/
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
size of array in c
提问by yotamoo
a simple question that bugs me.
Say I have an array defined in main like so int arr[5]. Now, if I'm still inside main and I set int i = sizeof(arr)/sizeof(arr[0])then I is set to be 5, but if I pass the array as a function parameter and do the exact same calculation in this function, I get a different number. Why is that? At first I thought its because in a function arris a pointer, but as far as I know arris a pointer inside main too!
一个让我烦恼的简单问题。假设我在 main 中定义了一个数组,就像这样int arr[5]。现在,如果我仍然在 main 中并且我设置了int i = sizeof(arr)/sizeof(arr[0])那么我设置为 5,但是如果我将数组作为函数参数传递并在这个函数中执行完全相同的计算,我会得到一个不同的数字。这是为什么?起初我认为它是因为在函数中arr是一个指针,但据我所知arr在 main 中也是一个指针!
Also, if I do something very similar only I initialize the array dynamically, I get weird results:
另外,如果我做一些非常相似的事情,只是我动态初始化数组,我会得到奇怪的结果:
int *arr = (int*) malloc(sizeof(int) * 5);
int length = sizeof(*arr) / sizeof(arr[0]);
printf("%d\n",length);
Here the output is 1. Any ideas why?
Thanks in advance!
这里的输出是1。任何想法为什么?提前致谢!
回答by Staven
C arrays don't store their own sizes anywhere, so sizeofonly works the way you expect if the size is known at compile time. malloc()is treated by the compiler as any other function, so sizeofcan't tell that arrpoints to the first element of an array, let alone how big it is. If you need to know the size of the array, you need to explicitly pass it to your function, either as a separate argument, or by using a struct containing a pointer to your array and its size.
C 数组不会将它们自己的大小存储在任何地方,因此sizeof只有在编译时已知大小的情况下才能按照您期望的方式工作。malloc()编译器将其视为任何其他函数,因此sizeof无法判断它arr指向数组的第一个元素,更不用说它有多大了。如果您需要知道数组的大小,则需要将其作为单独的参数显式传递给函数,或者使用包含指向数组及其大小的指针的结构。
回答by user151019
This is because arr is now a pointer and it could point to a single int or an array of 1000 ints the function just does not know. You will have to pass the size of the array into the function.
这是因为 arr 现在是一个指针,它可以指向单个 int 或函数不知道的 1000 个 int 数组。您必须将数组的大小传递给函数。
In main arr is declared as an int[5] and so the size can be calculated by the compiler.
在 main arr 中声明为 int[5],因此编译器可以计算大小。
回答by paercebal
I didn't understand the first question (post some code, please), but for the second:
我不明白第一个问题(请发布一些代码),但对于第二个问题:
sizeof(*arr) ; // is sizeof(int) as arr is of type "int *"
sizeof(arr[0]) ; // is sizeof(int), as arr[0] is the first
// item of an array of int
Fact is, *arrand arr[0]mean exactly the same thing, but said differently. In fact, let say n is an index: *(arr + n)is the same than arr[n].
事实是,*arr和arr[0]意思完全一样,只是说法不同。事实上,假设 n 是一个索引:*(arr + n)与 相同arr[n]。
回答by Steve Jessop
as far as I know arr is a pointer inside main too!
据我所知, arr 也是 main 中的一个指针!
That's the mistake. In main, where you defined int arr[5], arris an array, not a pointer. Its size is equal to the size of the array, so 5*sizeof(int). When you passed it as a function parameter, that parameter had type int*, so inside the function, you were taking the size of an int*rather than an int[5].
这就是错误。在main你定义的地方int arr[5],arr是一个数组,而不是一个指针。它的大小等于数组的大小,所以5*sizeof(int). 当您将它作为函数参数传递时,该参数具有 type int*,因此在函数内部,您采用的是 anint*而不是 an的大小int[5]。
Here the output is 1. Any ideas why?
这里的输出是 1。任何想法为什么?
This time, arrreally is a pointer, since you declared it int *arr. But either way, whether it's int *arror int a[5], *arrand arr[0]mean exactly the same thing: the first element. So of course they have the same size, and sizeof(*arr) / sizeof(arr[0])is 1.
这一次,arr真的是一个指针,因为你声明了它int *arr。但无论哪种方式,无论是int *arr或int a[5],*arr并arr[0]意味着完全一样的东西:第一个元素。所以当然它们具有相同的大小,并且sizeof(*arr) / sizeof(arr[0])是 1。
回答by David van Laatum
It's because of the difference between an array of known size and a pointer to an array of unknown size. sizeofis done at compile time and it's not possible for the compiler to tell the size of a dynamically created memory region in advance.
这是因为已知大小的数组和指向未知大小数组的指针之间存在差异。sizeof是在编译时完成的,编译器不可能提前告诉动态创建的内存区域的大小。
回答by Shahbaz
You should understand the difference between static and dynamic arrays. Static arrays are types like int, float, doubleetc. They are different. Even
您应该了解静态和动态数组之间的区别。静态数组类型,如int,float,double等它们是不同的。甚至
int a[10];
has a different type from
有不同的类型
int b[11];
At compile time, the number of elements in static arrays are known and the sizeofoperator returns the number of bytes they occupy.
在编译时,静态数组中的元素数量是已知的,并且sizeof运算符返回它们占用的字节数。
With pointers that were initialized, either to point to some variable, or to an allocated memory, it is at run-time that it would be apparent where they are pointing to and the compiler cannot determine what would be the size of that array in the future. Therefore, the sizeofa pointer gives you 4 (or 8 in 64bits systems for example).
对于已初始化的指针,要么指向某个变量,要么指向已分配的内存,在运行时,它们指向的位置很明显,编译器无法确定该数组的大小未来。因此,sizeofa 指针为您提供 4 个(例如,在 64 位系统中为 8 个)。
Note that sizeofoperator works at compile-time and not at run-time.
请注意,sizeof运算符在编译时工作,而不是在运行时工作。
If you need to know the size of an allocated memory (through malloc), you have no choice but to save the size of the array in another variable, for example right after you did the malloc.
如果您需要知道分配的内存的大小(通过malloc),您别无选择,只能将数组的大小保存在另一个变量中,例如在您执行malloc.

