2

If I have a pointer like:

int* ptr;

and I do:

printf("%#x\n%#x\n%#x\n", ptr, ptr+1, ptr+2);

I get the output as:

some address
some address + 4bytes
some address + 8bytes

Now if I make the pointer short int* ptr

I print in the same way as above and get the output as:

some address
some address + 2bytes
some address + 4bytes

Why is that? Aren't addresses unsigned integers? If so, then the datatype to which a pointer is pointing to should not matter. The pointer will always store an address which is an unsigned int hence it would occupy 4 bytes. Why is a short int pointer occupying 2bytes whereas an int pointer is occupying 4bytes? In the end, both pointers store addresses only, isn't it?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
user904832
  • 557
  • 1
  • 6
  • 10
  • possible duplicate of [Pointer Arithmetic](http://stackoverflow.com/questions/394767/pointer-arithmetic) – Aamir Feb 11 '12 at 10:43
  • I get how pointer arithmetic works. I just don't understand why it works the way it works. – user904832 Feb 11 '12 at 10:51
  • This is a feature of compiler. It counts at compilation time the number of bytes to add it to the pointer value. – mikithskegg Feb 11 '12 at 10:55
  • It's UB to print addresses with `"%x"`. To be portable and Standard compliant you need `"%p"` and cast the address to `void*`. – pmg Feb 11 '12 at 11:05
  • all of the values above are unsigned integers. Can you point out any float here?. BTW you should accept the correct answer – phuclv Oct 17 '13 at 11:35

5 Answers5

3

Pointer arithemtic (i.e. ptr+n) is performed in units of the the thing being pointed to.

Remember than ptr+n is equivalent to &ptr[n], so it's also equivalent to:

(T *)((char *)ptr + n*sizeof(T))

where T is whatever type you're pointing to.


By the way, you should be using %p to display pointers, not %#x.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

If you do

int* ptr;
printf("%#x\n%#x\n%#x\n", ptr, ptr+1, ptr+2);

The compiler says "Hey, ptr is pointing to some int, and the programmer wants the ints at offsets of 1 resp. 2 ints. So, I get sizeof(int) (which is on many architectures, including yours, 4 bytes) and add it to the value of ptr". So the output will be offsets by 4 bytes.

Layout in memory:

ptr --+
      |
      v
      +---------+---------+---------+
      |   int   |   int   |    int  |
      +---------+---------+---------+
        4 bytes   4 bytes   4 bytes

When doing

unsigned int* ptr;
printf("%#x\n%#x\n%#x\n", ptr, ptr+1, ptr+2);

The compiler says "Hey, ptr is pointing to some unsigned int, and the programmer wants the unsigned ints at offsets of 1 resp. 2 ints. So, I get sizeof(unsigned int) (which is on many architectures, including yours, 2 bytes) and add it to the value of ptr". So the output will be offsets by 2 bytes.

Layout in memory:

ptr --+
      |
      v
      +---------+---------+---------+
      |unsigned | unsigned|unsigned |
      +---------+---------+---------+
        2 bytes   2 bytes   2 bytes
phimuemue
  • 34,669
  • 9
  • 84
  • 115
0

yes pointers are unsigned integers ,Now Considering definition int *ptr

ptr dont represent the address of pointer , it represent the address of the variable that pointer is pointing and size will be dependent on the type of the variable it is pointing towards. thou if you did something like this for pointer pointing towards any type , printf("%x %x %x",&ptr,&ptr+1,&ptr+2) , then the difference between addrresses would be same

parth_07
  • 1,322
  • 16
  • 22
0

Pointer arithmetics differs from usual integer arithmetics. Compiler has in mind length of a variable type this pointer points to and adds to the address this length, not just a number 1, 2 and so on

mikithskegg
  • 806
  • 6
  • 10
  • If a pointer pointing to a short int is advanced by 2bytes everytime then doesn't that mean that the pointer length is 2bytes? If it is indeed 2bytes then how will it store an address that is greater than 2^16? – user904832 Feb 11 '12 at 10:49
  • No, certainly length of a pointer is not equal to the length of data it points to. Werther I said not clear or you didn't understand me. ))) – mikithskegg Feb 11 '12 at 10:54
  • I get it now. I kept confusing the pointer size with the size of the datatype it is pointing to. Thanks! – user904832 Feb 11 '12 at 10:58
0

You are right, pointers store addresses. However when you say int* ptr, it allocates 4 bytes in the memory, so that any new allocation will not touch this area. Similarly short occupies 2 bytes hence it allocates 2 bytes in memory.

Bottom line: Pointers store address value which is common to all datatypes, but the capacity varies

You may read this: http://www.taranets.net/cgi/ts/1.37/ts.ws.pl?w=329;b=279

Jayy
  • 2,368
  • 4
  • 24
  • 35