整数类型的指针大小与int *大小 [英] Size of pointer of integer type vs Size of int*
问题描述
我开始阅读Pointers并与此同时进行修改.我偶然发现了这个:
I started reading Pointers and while tinkering with them. I stumbled upon this :
#include<stdio.h>
int main()
{
int *p,a;
a=sizeof(*p);
printf("%d",a);
}
输出:4
然后用sizeof(int*)
代替sizeof(*p)
,现在输出8.
Then in the place of sizeof(*p)
I replaced it with sizeof(int*)
Now it outputs 8 .
P是整数类型的指针,并且int *也是同一件事(我的假设正确吗?).然后为什么要打印两个不同的值.我正在64位gcc编译器上执行此操作.
P is a pointer of integer type and int* is also the same thing ( Is my assumption correct? ). Then why it is printing two different values. I am doing this on a 64bit gcc compiler.
推荐答案
每个初学者总是对指针声明和取消引用指针感到困惑,因为语法看起来是一样的.
Every beginner always gets confused with pointer declaration versus de-referencing the pointer, because the syntax looks the same.
-
int *p;
的意思是声明一个指向int的指针".您也可以将其写为int* p;
(含义相同,个人喜好). -
*p
,在声明中其他位置使用时,表示取p指向的内容".
int *p;
means "declare a pointer to int". You can also write it asint* p;
(identical meaning, personal preference).*p
, when used anywhere else but in the declaration, means "take the contents of what p points at".
因此,sizeof(*p)
的意思是给我p指向的内容的大小",但是sizeof(int*)
的意思是给我指针类型本身的大小".在您的计算机上,int
显然是4个字节,但指针是8个字节(通常是64位计算机).
Thus sizeof(*p)
means "give me the size of the contents that p points at", but sizeof(int*)
means "give me the size of the pointer type itself". On your machine, int
is apparently 4 bytes but pointers are 8 bytes (typical 64 bit machine).
这篇关于整数类型的指针大小与int *大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!