【发布时间】:2021-03-29 11:01:58
【问题描述】:
我编写了这个非常简单的程序来检查 c 中整数指针的大小。
#include <stdio.h>
int main(void) {
int i = 10;
int* i_ptr = &i;
int arr[3] = {13, 14, 15};
printf("\n ... Size of an integer pointer is: %ld bytes and %ld bits ...", sizeof(i_ptr), 8*sizeof(i_ptr));
printf("\n\n ... Size of a 3-element integer array is: %ld bytes and %ld bits ...\n\n", sizeof(arr), 8*sizeof(arr));
printf(" ... Size of the *arr element is: %ld bytes and %ld bits ...\n\n", sizeof(*arr), 8*sizeof(*arr));
return 0;
}
关于不同变量大小的输出是:
... Size of an integer pointer is: 8 bytes and 64 bits ...
... Size of a 3-element integer array is: 12 bytes and 96 bits ...
... Size of the *arr element is: 4 bytes and 32 bits ...
现在,我的问题如下。当我运行 64 位 linux 操作系统时,我知道整数指针变量的大小是 64 位长。整数数组的大小为 12 个字节,因为整数占用 4 个字节的内存,而数组由 3 个整数组成。但是,当我认为数组基本上是 C 中的指针并且数组的变量指向数组的第一个元素时,我认为数组的大小将类似于相应指针的大小。然而,我也明白这并不意味着数组的大小将是 3 个整数指针的大小一样长的 24 个字节。这对我来说似乎有点令人困惑,所以任何建议都将不胜感激。
【问题讨论】:
-
" 数组基本上是 C 中的指针" - 这根本不正确。数组可以衰减为指针,但它们“基本相同”
标签: arrays c pointers implicit-conversion sizeof