简体   繁体   English

sizeof()如何通过引用参数传递

[英]how sizeof() works in pass by reference arguments

I passed a array to function and tried to find the length of the array . 我将数组传递给函数,并试图找到数组的长度。 but the result was not expected . 但结果出乎意料。 can anybody explain please? 有人可以解释吗?

int main()
{
     int array[10]={0};
     func(array);
     return 0;
}
void func(int arr[])
{
    printf("length of an array is %d ",(sizeof(arr)/sizeof(arr[0]));
}

it gives the answer 2. when I tried the same operation inside main function it works fine (answer is 10). 它给出了答案2.当我在main函数中尝试相同的操作时,它工作正常(答案为10)。 //using gcc complier in linux //在Linux中使用gcc编译器

Seems to me that the result is caused because sizeof(arr) == 8 (size of a pointer on your PC) and sizeof(arr[0]) == 4 because it is an integer hence 8/4==2 . 在我看来,造成此结果的原因是sizeof(arr) == 8 (PC上指针的大小)和sizeof(arr[0]) == 4因为它是整数,因此是8/4==2

This declaration: void func(int arr[]) tells the function to expect a pointer to int as argument. 该声明: void func(int arr[])告诉函数将指向int的指针作为参数。

It is not clear to me whether is possible to calculate the sizeof an array by reference. 我不清楚是否可以通过引用来计算数组的sizeof C functions accepting array reference as arguments always tend to receive their length too as argument. 接受数组引用作为参数的C函数总是倾向于也接收其长度作为参数。

The difference with main() function is that inside main array variable is of type int[10] , thus sizeof is able to get its length in bytes. main()函数的区别在于main array内部变量的类型为int[10] ,因此sizeof可以获取其长度(以字节为单位)。 In func , arr is of type int* so sizeof gives you only the length in bytes of a pointer. funcarr的类型为int*因此sizeof仅提供指针的字节长度。

You are sending and recieving a array pionter, not the array. 您正在发送和接收数组标兵,而不是数组。

while sending arg:: 在发送arg ::时

func(array[10]);

While Receiving arg: 收到arg时:

void func(int array[10])

But it's not good to send the total array. 但是发送总数组不是很好。 So send and receive arguments like below. 因此,发送和接收如下参数。

func(array, arry_size);  //function call
void func(int array[], int length)  //defin

You have to remember that arrays decays to pointers. 您必须记住,数组会衰减到指针。 That means that in the function func the variable arr is not actually an array but a pointer. 这意味着在函数func ,变量arr实际上不是数组,而是指针。 And doing sizeof on a pointers returns the size of the pointer and not what it points to. 对指针执行sizeof返回指针的大小,而不是其指向的大小。

You get the result 2 because you're on a 64-bit system where pointers are 64 bits (ie 8 bytes), an when divided by the size of int (which is 4 bytes) you get the result 2 . 之所以得到结果2是因为您使用的是64位指针(即8个字节)的64位系统,将其除以int的大小(即4个字节)就得到了结果2

sizeof(arr)

是指针的大小,而不是arr指向的块的大小。

When you pass an array into a function as a parameter, the array decays into a pointer to the beginning of the array. 当您将数组作为参数传递给函数时,该数组会衰减为指向数组开头的指针。 A pointer has no meta information stored in it unlike the array. 与数组不同,指针中没有存储任何元信息。 A good practice is to pass the size to the function as an additional parameter. 好的做法是将大小作为附加参数传递给函数。

void func(int *arr, int size);

Here's what C standard says (C99 6.3.2.1/3 - Other operands - Lvalues, arrays, and function designators): 这就是C标准所说的内容(C99 6.3.2.1/3-其他操作数-左值,数组和函数指示符):

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue 除非它是sizeof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则将类型为“ array of type”的表达式转换为类型为“ pointer to to”的表达式类型'',它指向数组对象的初始元素并且不是左值

So, once you pass it to the function - Inside the function, it's no more an array, it's a pointer. 因此,一旦将其传递给函数-在函数内部,它不再是数组,而是指针。

sizeof() operator returns the size of the data type. sizeof()运算符返回数据类型的大小。 here int size is two. 这里的int大小是两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM