[英]How many elements does the compiler take from an array which is function argument?
I have heard that when using an array as a function argument, the compiler takes it as not actually an array but as a pointer to the 1st element of the array.我听说当使用数组作为 function 参数时,编译器将其视为实际上不是数组,而是作为指向数组第一个元素的指针。 So, when asking for size of A(int array) it should return size of pointer 4 (in bytes).因此,当询问 A(int array) 的大小时,它应该返回指针 4 的大小(以字节为单位)。 But I am getting 8. What is the problem?但我得到8。有什么问题?
I am using CodeBlocks.我正在使用代码块。
int SumOfElemets(int* A, int size){// int* A or int A[] ... it is the same!
int i, sum = 0;
printf("SOE - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));
//here it should be 4 and 4 but i am getting 8 and 4
for (i=0;i<size; i++){
sum += A[i]; //A[i] is *(A+i)
}
return sum;
}
int main(){
int A[] = {1,2,3,4,5};
int size = sizeof(A)/sizeof(A[0]); //size of the array
int total = SumOfElemets(A, size);
printf("Sum of elements = %d\n", total);
printf("Main - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));
return 0;
}
Hi I have heard that when using an array as a function argument compiler takes it as not actually an array but as a pointer to the 1st element of the array.嗨,我听说当使用数组作为 function 参数编译器时,它实际上不是数组,而是指向数组第一个元素的指针。
Firstly function parameters having array types are adjusted by the compiler to pointers to the array element types.首先,编译器将具有数组类型的 function 参数调整为指向数组元素类型的指针。
For example these function declarations例如这些 function 声明
int SumOfElemets(int* A, int size);
int SumOfElemets(int A[], int size);
int SumOfElemets(int A[1], int size);
int SumOfElemets(int A[100], int size);
are equivalent and declare the same one function.是等效的并声明相同的 function。
On the other hand, arrays used in expressions as for example argument expressions with rare exceptions are implicitly converted to pointers to their first elements.另一方面,在表达式中使用的 arrays (例如具有罕见异常的参数表达式)被隐式转换为指向其第一个元素的指针。
So these function calls所以这些 function 调用
SumOfElemets(A, size);
SumOfElemets(&A[0], size);
SumOfElemets(A + 0, size);
are equivalent.是等价的。
The size of pointer is implementation defined as by the way the size of an object of the type int
though usually sizeof( int )
or in the case of your code sizeof( A[0] )
is equal to 4.指针的大小被实现定义为int
类型的 object 的大小,尽管通常sizeof( int )
或在您的代码的情况下sizeof( A[0] )
等于 4。
Pay attention to that expression with the sizeof
operator has the type size_t
.请注意带有sizeof
运算符的表达式的类型为size_t
。 So you need to use the conversion specifier %zu
to output the value of the expression instead of %d
所以你需要使用转换说明符%zu
到 output 表达式的值而不是%d
printf("SOE - Size of A = %zu, size of A[0] = %zu\n", sizeof(A), sizeof(A[0]));
The sizeof
operator yields the size (in bytes) of its operand. sizeof
运算符产生其操作数的大小(以字节为单位)。
When you call sizeof(A)
, that means "what is the size of the pointer A
?".当您调用sizeof(A)
时,这意味着“指针A
的大小是多少?”。
That is different from sizeof(A[0])
, which means "what's the size of the first element of the array A
?".这与sizeof(A[0])
不同,这意味着“数组A
的第一个元素的大小是多少?”。
The size of types is compiler-dependent , and in C implementations with a 64-bit address space, pointers
are typically 64 bits (eight 8-bit bytes) and int
are often 32 bits, therefore:类型的大小取决于编译器,并且在具有 64 位地址空间的 C 实现中, pointers
通常是 64 位(八个 8 位字节),而int
通常是 32 位,因此:
sizeof(A)
; sizeof(A)
8 个字节;sizeof(A[0])
. sizeof(A[0])
4 个字节。(Clarified byEric Postpischil ) (由Eric Postpischil澄清)
Check out the following examples:查看以下示例:
Example with a x64 ( 64bit address space) compiler: https://godbolt.org/z/vYcWhvxsE使用x64 ( 64 位地址空间)编译器的示例:https://godbolt.org/z/vYcWhvxsE
Output: Output:
Size of pointer to int (x64): 8
Size of integer (x64): 4
Example with a x86 ( 32bit address space) compiler: https://godbolt.org/z/68PTsM6db使用x86 ( 32 位地址空间)编译器的示例:https://godbolt.org/z/68PTsM6db
Output: Output:
Size of pointer to int (x86): 4
Size of integer (x86): 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.