Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中不一致的sizeof行为_C_Arrays_Sizeof - Fatal编程技术网

C中不一致的sizeof行为

C中不一致的sizeof行为,c,arrays,sizeof,C,Arrays,Sizeof,可能重复: 有人能解释一下为什么下面的C代码会这样做: #include <stdio.h> int sizeof_func(int data[]) { return sizeof(data); } int main(int argc, char *argv[]) { int test_array[] = { 1, 2, 3, 4 }; int array_size = sizeof(test_array); printf("size of te

可能重复:

有人能解释一下为什么下面的C代码会这样做:

#include <stdio.h>

int sizeof_func(int data[]) {
    return sizeof(data);
}

int main(int argc, char *argv[]) {
    int test_array[] = { 1, 2, 3, 4 };
    int array_size = sizeof(test_array);
    printf("size of test_array : %d.\n", array_size);
    int func_array_size = sizeof_func(test_array);
    printf("size of test_array from function : %d.\n",
        func_array_size);
    if (array_size == func_array_size) {
        printf("sizes match.\n");
    } else {
        printf("sizes don't match.\n");
    }
    return 0;
}
但我得到的却是:

size of test_array : 16.
size of test_array from function : 4.
sizes don't match.

当您将数组作为函数参数传递时,它将衰减为指向其第一个元素的指针。
函数中的
sizeof
返回指针的大小,而不是数组的大小。
main()
中的
sizeof
返回数组的大小。当然,两者并不相同

如果想知道函数中数组的大小,必须将其作为单独的参数传递给函数

int sizeof_func(int data[], size_t arrSize);
                            ^^^^^^^^^^^^

您的功能相当于:

int sizeof_func(int *data) {
    return sizeof(data);
}
函数无法知道传递给它的数组的大小,因为实际上传递给它的只是指向数组第一个元素的指针。您使用的是32位系统,因此
sizeof(int*)
为4

事实上,
sizeof(…)
是一个在编译时计算的常量,因此您的
sizeof_funct
无法工作。相反,人们通常在C中使用指针传递一个整数,如下所示:

int function_that_operates_on_array(int *data, int size)
{
    // do stuff with data[0] through data[size-1]
}
在这里,编译器将
test\u数组
视为一个数组(它知道在编译时数组的实际大小),这就是为什么要得到
test\u数组
的真实大小

   int func_array_size = sizeof_func(test_array);
   printf("size of test_array from function : %d.\n",
   func_array_size);

但是,如果将数组传递给函数,编译器会将其视为指向数组第一个元素的指针(在编译时,函数不知道数组的大小,因为可以使用先前声明的任何数组调用函数)这就是为什么要得到指针的大小。

函数的定义是

int-sizeof_func(int-data[]

{

返回sizeof(数据)

}

这对于您的情况是不正确的,因为您在函数调用中将指向数组第一个元素的指针作为参数传递。 e、 g.int func_数组_size=sizeof_func(测试数组)

修改函数定义,如下所示:

int sizeof_func(int*data,int arraysize)

{

}

并将函数调用修改为, int func_数组_size=sizeof_func(测试数组,4)


这应该行得通。

有两个C语言问题需要注意

1) sizeof运算符的行为。它仅适用于静态大小的数组。如果您有
sizeof(test\u array)
,那么它将适用于实际阵列:

int test_array[]  = { 1, 2, 3, 4 }; 
int test_array[4] = { 1, 2, 3, 4 }; // completely equivalent
char test_array[] = "something";
当您向它传递指针时,它将不起作用。如果这样做,您将获得指针的大小。以下情况不起作用,它们将打印4(假定为32位):


2) 函数参数的行为。它们在C中有一个奇怪的语法。它们可以声明为数组,但它们不是:

void func (int* x);    // x is a pointer
void func (int x[]);   // x is a pointer
void func (int x[10]); // x is a pointer
以上3个是等价的,后两个只是“合成糖”,用来告诉和描述程序员的意图

在所有这些情况下,任何调用sizeof(x)的尝试都会给出指针的大小。

关于此主题的阅读非常好。
 return (sizeof(data) * arraysize); 
int test_array[]  = { 1, 2, 3, 4 }; 
int test_array[4] = { 1, 2, 3, 4 }; // completely equivalent
char test_array[] = "something";
int*  pointer = test_array;
char* pointer = "something";
printf("%d", sizeof(pointer));
void func (int* x);    // x is a pointer
void func (int x[]);   // x is a pointer
void func (int x[10]); // x is a pointer