Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.I have the below program in C and I want your help to understand something.
Code:

What I have tried:

C++
    char x, s[5], *p = NULL;
    int y, a[10], *q = NULL;p = &x;
    q = a;
    printf("char: %ld, int: %ld\n",  sizeof(char), sizeof(int));
    printf("x: %ld, y: %ld\n",  sizeof(x), sizeof(y));
    printf("s: %ld, a: %ld\n",  sizeof(s), sizeof(a));
    printf("p: %ld, q: %ld\n",  sizeof(p), sizeof(q));
    printf("*p: %ld, *q: %ld\n",  sizeof(*p), sizeof(*q));
}



with output:
C++
char: 1, int: 4
x: 1, y: 4
s: 5, a: 40
p: 8, q: 8
*p: 1, *q: 4


The only thing I can not understand why is happening is the p: 8, q: 8
64-bit system
Why?
Thanks.
Posted
Updated 26-Oct-20 6:38am

1 solution

p and q are both pointers and on a 64-bit system a pointer is 8 bytes or 64-bits long. The data they point to has a different size in their cases but that is irrelevant to the size of the pointer. A pointer will always be eight bytes long on a 64-bit system regardless of what it points to.
 
Share this answer
 
v2
Comments
Nick_is_asking 26-Oct-20 12:46pm    
I understand.Thank you!
CPallini 26-Oct-20 14:04pm    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900