-3

I went through pointer arithmetic and the fact that you can't assign pointer of one data type to another data type. for example, below declaration is incorrect.

  double x = 10;
  int *ptrInt = &x;

We've assigned the address of double variable to a "pointer to integer". Double takes 8 bytes as compared to an integer, that takes 4 bytes and therefore an integer pointer will truncate those extra 4 bytes.

But how come, the size of any pointer variable is 8 bytes and that also means it will not truncate those extra 4 bytes and should work correctly(even though it doesn't).

I have this doubt. Can anybody help me with the clarification?

yogi rajput
  • 1
  • 1
  • 7
  • The pointer is the address. The variable (x) is what lives at that address. If you assign the address of a double to an int pointer and dereference the int pointer, you're going to get sizeof(int) bytes out. It's a strange workflow, so you have to jump through some hoops to say "yes, I really want to do this". And you probably don't – Millie Smith Sep 15 '18 at 05:28
  • 3
    The size of a pointer has nothing to do with the size of the object it points to. A 64-bit pointer supports a 64-bit address space. For instance, suppose you had an array of 2**34 character variables. You would need a minimum of 34 address bits to address every element of the array, even though the elements themselves are only 1 byte each. – Tom Karzes Sep 15 '18 at 05:28
  • https://stackoverflow.com/a/17260931/2850543 – Millie Smith Sep 15 '18 at 05:33
  • Think about `printf ("pointer size: %lu-bytes\n", sizeof (void*));` – David C. Rankin Sep 15 '18 at 07:04

1 Answers1

4

how come, the size of any pointer variable is 8 bytes ? pointer variable contains address & size of address is 8 byte on 64-bit system irrespective of whether pointer variable points to int or char or float objects as 64-bit pointer supports 8 byte address space.

Achal
  • 11,821
  • 2
  • 15
  • 37