数组长度问题

发布于 2024-12-04 01:22:26 字数 285 浏览 2 评论 0原文

我正在阅读一份 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

薄荷→糖丶微凉 2024-12-11 01:22:26

因为int array[]只是一个指针,它的大小与int相同。
我认为您期望 arr 的大小会以某种方式传递给函数,但事实并非如此。 arr 的大小只能在声明它的同一范围内确定,因为 sizeof 实际上在编译时起作用。

Because int array[] is just a pointer and it's size is same as of int.
I think you expected that size of arr will somehow be passed to function, but it doesn't work that way. Size of arr can be determined only in same scope where it was declared, because sizeof actually works at compile time.

流云如水 2024-12-11 01:22:26

该数组作为指针传递。该函数无法知道分配给数组的空间有多少。

The array is passed as a pointer. There is no way for the function to know how much space was allocated to the array.

っ左 2024-12-11 01:22:26

所有数组在传递时都会衰减为指针,因此表达式变为:
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 ints and 2 on 64bit machines with 32bit ints.

〆一缕阳光ご 2024-12-11 01:22:26

您需要将长度传递给函数,或者创建一个包含指向数组的指针和数组长度的结构。 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.

伤痕我心 2024-12-11 01:22:26

C 中的数组是一种非常脆弱的类型。在很多情况下,数组会衰减为指向第一个元素的指针,将数组作为函数的参数传递就是这种情况。看哪:

int main()
{
  char data[10];        // sizeof(data) == 10
  return call_me(data); // data is equivalent to &data[0] here!
}

int call_me(char x[])
// int call_me(char * x)   // same thing!
{
  // here sizeof(x) == sizeof(char *)
  // ...
}

数组在 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:

int main()
{
  char data[10];        // sizeof(data) == 10
  return call_me(data); // data is equivalent to &data[0] here!
}

int call_me(char x[])
// int call_me(char * x)   // same thing!
{
  // here sizeof(x) == sizeof(char *)
  // ...
}

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); Here p 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.)

樱花落人离去 2024-12-11 01:22:26

请参阅函数定义的语法,可以按照以下方式编写

 int DIMension(int array[]) {
 return sizeof(array )/ sizeof(int);
 }


 int DIMension(int *array) 
 {
 return sizeof(array )/ sizeof(int);
 }

int DIMension(int array[10])
{
return sizeof(array )/ sizeof(int);
}

所有这三个语句具有相同的含义,并且三个语句的共同点是参数数组,它只不过是一个指向数组的指针,该数组始终为 4 个字节

See the syntax for the function defination can be written following way

 int DIMension(int array[]) {
 return sizeof(array )/ sizeof(int);
 }


 int DIMension(int *array) 
 {
 return sizeof(array )/ sizeof(int);
 }

int DIMension(int array[10])
{
return sizeof(array )/ sizeof(int);
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文