-1

Why is that all the pointers in C have same size? I am on 64 bit arch.

#include<stdio.h> int main(){ printf("int\t%ld\n",sizeof(int*)); printf("char\t%ld\n",sizeof(char*)); printf("void\t%ld\n",sizeof(void*)); printf("float\t%ld\n",sizeof(float*)); } OP : int 8 char 8 void 8 float 8 
13
  • 1
    Why wouldn't they have the same size? Commented Jul 17, 2016 at 14:59
  • They are not guaranteed to be same size but generally always are: stackoverflow.com/questions/1241205/… Commented Jul 17, 2016 at 15:01
  • 1
    Suppose one of them had a smaller size, that would put a big limit on where things of that type are allowed to exist in memory. Commented Jul 17, 2016 at 15:06
  • @harold: Which is typical on some architectures. There is more to C than x86/ARM. Commented Jul 17, 2016 at 15:51
  • @Olaf it is a consequence, I didn't say that makes it impossible. It sure makes it inconvenient though. Commented Jul 17, 2016 at 15:55

1 Answer 1

4

Because all you need is 64 bits to index a memory address and all the data types you've listed only need to index a memory address (where the given data 'starts' in memory). Note that this is true for C, but in C++ e.g., member function pointers might be more than that.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes they all point to a memory address. The reason they are still of different datatype like int char etc is for C compiler to do Pointer Arithmetic. For example adding a 'char' pointer would increment by one signifying next byte and four for a 'int'.
@Nishant: that's a consequence of the reason, but not the reason. Had it been the reason, for any T and U where sizeof(T) == sizeof(U), we could've said that T* is the same as U* - which is clearly not the case for e.g. different structs. The reason is type safety and the need for dereference operator to have a definite, specific return type. A consequence is that the compiler can now do meaningful pointer arithmetics.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.