sizeof 运算符的使用
以下程序的输出
#include<stdio.h>
int main(){
int *p[10];
printf("%ld %ld\n",sizeof(*p),sizeof(p));
}
现在
8 <--- sizeof(*p) gives size of single element in the array of int *p[10]
80 <--- sizeof(p) gives size of whole array which is 10 * 8 in size.
看到以下程序
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
printf("sizeof(array) = %ld \n",sizeof(array));
printf("sizeof(array[0]) = %ld \n",sizeof(array[0]));
printf("sizeof int %ld\n",sizeof(int));
printf("TOTAL_ELEMENTS=%ld \n",TOTAL_ELEMENTS);
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
return 0;
}
是
sizeof(array) = 28
sizeof(array[0]) = 4 <--here
sizeof int 4
TOTAL_ELEMENTS=7
我无法理解的是为什么两个输出中的 sizeof(array[0]) 不同。
The output of following program
#include<stdio.h>
int main(){
int *p[10];
printf("%ld %ld\n",sizeof(*p),sizeof(p));
}
is
8 <--- sizeof(*p) gives size of single element in the array of int *p[10]
80 <--- sizeof(p) gives size of whole array which is 10 * 8 in size.
now see the following program
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
printf("sizeof(array) = %ld \n",sizeof(array));
printf("sizeof(array[0]) = %ld \n",sizeof(array[0]));
printf("sizeof int %ld\n",sizeof(int));
printf("TOTAL_ELEMENTS=%ld \n",TOTAL_ELEMENTS);
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
return 0;
}
is
sizeof(array) = 28
sizeof(array[0]) = 4 <--here
sizeof int 4
TOTAL_ELEMENTS=7
What I am not able to understand is why is the sizeof(array[0]) different in both the outputs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
int *p[10];
是一个指针数组。
*p
是该指针数组的第一个元素。所以它是一个指向整数的指针。它不是一个整数。
int array[] = {23,34,12,17,204,99,16};
是一个整数数组。因此 array[0] 是该数组的第一个元素。所以它是一个整数。指向整数 (
*p
) 和整数 (array[0]
) 的指针的大小不同。所以
sizeof(*p)
和sizeof(array[0])
是不同的。
sizeof(p)
给出指针数组的大小。所以它是:10 x 8 = 80。即 (元素数量) x (一个元素的大小)
sizeof(array)
给出整数数组的大小。所以它是:7 x 4 = 28。int *p[10];
is an array of pointers.
*p
is the first element of that array of pointers. So it is a pointer to an integer. It is not an integer.
int array[] = {23,34,12,17,204,99,16};
is an array of integers. Soarray[0]
is the first element of that array. So it is an integer.The size of a pointer to an integer (
*p
) and an integer (array[0]
) are different.So
sizeof(*p)
andsizeof(array[0])
are different.
sizeof(p)
gives the size of the array of pointers. So it is: 10 x 8 = 80.i.e. (number of elements) x (size of one element)
sizeof(array)
gives the size of the array of integers. So it is: 7 x 4 = 28.在第一个示例中,元素是一个指向 int 的指针,而在第二个示例中它只是 int。你可以看到指针有8个字节,int只有4个字节。
In the first example, the element is a pointer to int, while in the second example it's just int. You can see that the pointer has 8 bytes, the int just 4 bytes.
在第一种情况下,您创建了一个指向 int 的指针数组,因此它们的大小是 8,而不是 4。
In the first case, you've created an array of pointers to int, so their size is 8, not 4.
在第一个示例中使用指针的大小,在第二个示例中使用整数的大小。它们可能具有不同的大小,尤其是在 64 位系统上。
In the first example the size of a pointer is used and in the second the size of an integer. They may have different sizes especially on 64 bit systems.
array[0]
的类型为int
*p
的类型为int*
这也许证明了写作风格上的
愚蠢比
第二个更清楚地表明 int* 是所声明的数组的类型。
array[0]
has typeint
*p
has typeint*
This perhaps demonstrates the stylistic folly of writing
rather than
where the second makes it clearer that int* is the type of the array being declared.
64位环境将int设置为32位,long并将指针设置为64位..
*p是一个指针 - 8字节
sizeof(p) - 是 10 个指针的大小 - 所以 80 字节
您有可能拥有 AMD64 机器 - 检查此以了解详细信息(包括其他选项)
http://gcc.gnu.org/onlinedocs/gcc/i386 -and-x86_002d64-Options.html
The 64-bit environment sets int to 32 bits and long and pointer to 64 bits ..
*p is a pointer - 8 bytes
sizeof(p) -is size of 10 pointers - so 80 bytes
Chances are you have AMD64 machine - check this for details (including other options)
http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html