数组长度问题
我正在阅读一份 csc 安置论文,其中我读到了一个与 c 语言数组的 sizeof() 运算符相关的问题。答案与我预期的不同。
int DIMension(int array[]) {
return sizeof(array )/ sizeof(int);
}
main() {
int arr[10];
printf(“Array dimension is %d”, DIMension(arr));
}
这个 C 语言程序打印 1 作为答案。为什么会发生这种情况?
I was reading a csc placement paper where I read a question related to c language array's sizeof() operator. Answer was something else then i expected it to be.
int DIMension(int array[]) {
return sizeof(array )/ sizeof(int);
}
main() {
int arr[10];
printf(“Array dimension is %d”, DIMension(arr));
}
This program in c language prints 1 as the answer. Why is that happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为
int array[]
只是一个指针,它的大小与int
相同。我认为您期望 arr 的大小会以某种方式传递给函数,但事实并非如此。
arr
的大小只能在声明它的同一范围内确定,因为sizeof
实际上在编译时起作用。Because
int array[]
is just a pointer and it's size is same as ofint
.I think you expected that size of
arr
will somehow be passed to function, but it doesn't work that way. Size ofarr
can be determined only in same scope where it was declared, becausesizeof
actually works at compile time.该数组作为指针传递。该函数无法知道分配给数组的空间有多少。
The array is passed as a pointer. There is no way for the function to know how much space was allocated to the array.
所有数组在传递时都会衰减为指针,因此表达式变为:
sizeof(void*)/sizeof(int)
这相当于在具有 32 位
int
的 32 位机器上为 1,在具有 32 位int
的 64 位机器上相当于 2。All arrays decay to pointers when they are passed around, so the expression becomes:
sizeof(void*)/sizeof(int)
which equates to 1 on 32bit machines with 32bit
int
s and 2 on 64bit machines with 32bitint
s.您需要将长度传递给函数,或者创建一个包含指向数组的指针和数组长度的结构。 C 数组不存储大小信息。
You need to pass the length to the function, or create a struct that includes both a pointer to the array and the length of the array. There is no size information stored with C arrays.
C 中的数组是一种非常脆弱的类型。在很多情况下,数组会衰减为指向第一个元素的指针,将数组作为函数的参数传递就是这种情况。看哪:
数组在 C 中很特殊,因为它们不能作为函数参数传递或从函数返回,因此当您期望数组时,您会经常看到指针。调用者(您!)有责任提供足够的信息来正确地将指针解释为数组。
请注意,常见的 C 习惯用法完全动态地创建“数组”,而无需提及数组类型:
char * p = malloc(10);
这里p
只不过是一个指针,并且完全取决于您是否记住可以将其视为数组。(C++ 中的情况要好一些,您可以通过引用传递实际的数组。)
An array in C is a very fragile type. There are many situations in which an array decays to a pointer to the first element, and passing an array as an argument of a function is such a situation. Behold:
Arrays are peculiar in C in the sense that they cannot be passed as function arguments or returned from functions, so you will frequently see pointers when you expect arrays. It is the responsibility of the caller (you!) to provide enough information to interpret a pointer as an array correctly.
Note that a common C idiom creates an "array" dynamically entirely without ever mentioning an array type:
char * p = malloc(10);
Herep
is never anything but a pointer, and it is entirely up to you to remember that you can treat it as an array.(The situation is a little better in C++, where you can pass actual arrays by reference.)
请参阅函数定义的语法,可以按照以下方式编写
所有这三个语句具有相同的含义,并且三个语句的共同点是参数数组,它只不过是一个指向数组的指针,该数组始终为 4 个字节
See the syntax for the function defination can be written following way
All these three statement has same meaning and common thing across the three is the argument array which is nothing but a pointer to array which is always gonna to be 4 bytes