Size of pointer of integer type vs Size of int*

后端 未结 2 1422
心在旅途
心在旅途 2021-01-03 01:13

I started reading Pointers and while tinkering with them. I stumbled upon this :

#include
int main()
{
    int *p,a;
    a=sizeof(*p);
    pri         


        
相关标签:
2条回答
  • 2021-01-03 01:28

    Every beginner always gets confused with pointer declaration versus de-referencing the pointer, because the syntax looks the same.

    • int *p; means "declare a pointer to int". You can also write it as int* p; (identical meaning, personal preference).
    • *p, when used anywhere else but in the declaration, means "take the contents of what p points at".

    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).

    0 讨论(0)
  • 2021-01-03 01:29

    *p and int* are not the same things! First one is a dereferenced pointer (i.e. int which is 4 bytes wide) and the second one is a pointer (8 bytes wide in your case since it's a 64 bit machine).

    0 讨论(0)
提交回复
热议问题