sizeof如何知道数组的大小? [英] How does sizeof know the size of array?

查看:542
本文介绍了sizeof如何知道数组的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

main() {
    int array[5] = {3,6,9,-8,1};
    printf("the size of the array is %d\n", sizeof(array));
    printf("the address of array is %p\n", array);
    printf("the address of array is %p\n", &array);
    int * x = array;
    printf("the address of x is %p\n", x);
    printf("the size of x is %d\n", sizeof(x));
}

输出为

the size of the array is 20
the address of array is 0x7fff02309560
the address of array is 0x7fff02309560
the address of x is 0x7fff02309560
the size of x is 8

我知道变量array将被视为指向数组第一个元素的指针,因此我知道x的大小为8.但是我不知道为什么数组的大小为20.它不是应该是8(在64位计算机上)吗?

I know the variable array will be seen as a pointer to the first element of the array, so I understand the the size of x is 8. But I don't know why the size of the array is 20. Isn't it should be 8 (in a 64-bits machine)?

除了程序如何知道它是20?据我所知,在C中它不存储元素的数量. sizeof(array)sizeof(x)有何不同?我跟踪了几篇有关数组衰减的文章,但对这个问题一无所知.

Besides how does the program know that it is 20? As far as I know in C it doesn't store the number of elements. How come the sizeof(array) and sizeof(x) is different? I tracked several posts pertaining to array decaying but no idea on this problem.

推荐答案

most 情况下,数组的名称会衰减为指向数组第一个元素的指针.但是,该规则有两个例外.最重要的两个是将数组名称用作sizeof运算符或address-of运算符(&)的操作数时.在这种情况下,数组的名称仍然是整个数组的标识符.

The name of an array decays to a pointer to the first element of the array in most situations. There are a couple of exceptions to that rule though. The two most important are when the array name is used as the operand of either the sizeof operator or the address-of operator (&). In these cases, the name of the array remains an identifier for the array as a whole.

对于非VLA数组,这意味着可以静态(在编译时)确定数组的大小,并且表达式的结果将是数组的大小(以字节为单位),而不是a的大小.指针.

For a non-VLA array, this means that the size of the array can be determined statically (at compile time) and the result of the expression will be the size of the array (in bytes), not the size of a pointer.

获取数组的地址时,您将获得相同的值(即,相同的地址),就好像您只是在不获取地址的情况下使用数组的名称一样.但是类型是不同的-当您显式地获取地址时,得到的是类型为指向N个类型为T的项的数组的指针"的指针.举例来说,这意味着array+1指向数组的第二个元素,而&array+1指向另一个数组,该数组恰好位于整个数组的末尾.

When you take the address of the array, you get the same value (i.e., the same address) as if you'd just used the name of the array without taking the address. The type is different though--when you explicitly take the address, what you get is a pointer of type "pointer to array of N items of type T". That means (for one example) that while array+1 points to the second element of the array, &array+1 points to another array just past the end of the entire array.

假设一个数组包含至少两个项目,*(array+1)将引用该数组的第二个元素.不管数组大小如何,&array+1都会产生一个超出数组末尾的地址,因此尝试取消引用该地址会产生未定义的行为.

Assuming an array of at least two items, *(array+1) will refer to the second element of the array. Regardless of the array size, &array+1 will yield an address past the end of the array, so attempting to dereference that address gives undefined behavior.

在您的情况下,假设数组的大小为20,并且数组的一个元素的大小为4,如果array为0x1000,则array+1将为0x1004&array+10x1014(0x14 = 20).

In your case, given that the size of the array is 20, and the size of one element of the array is 4, if array was, say, 0x1000, then array+1 would be 0x1004 and &array+1 would be 0x1014 (0x14 = 20).

这篇关于sizeof如何知道数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆