Reputation: 617
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?
Upvotes: 2
Views: 5902
Reputation: 1408
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
Upvotes: 0
Reputation: 36031
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 int
s 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 int
s 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
Upvotes: 1
Reputation: 2436
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
Upvotes: 0
Reputation: 272657
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
.
Upvotes: 3
Reputation: 816
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
Upvotes: 0