Reputation: 2455
I am confused about pointer pointing to address of variable
it points to last two bytes how does this work
#include <iostream>
using namespace std;
int main()
{
int i = 1;
short *j = (short*)&i;
cout << *j << endl;
}
.
Upvotes: 4
Views: 432
Reputation: 490158
A pointer normally holds the address of the beginning of the referred-to item.
From the sound of things, you're apparently using a little-endian system [edit: which is no surprise -- just for example, current (Intel) Macs and all Windows machines are little-endian], which means the least significant byte of your 4-byte int comes first in memory instead of last:
0000001 00000000 00000000 00000000
When you use a pointer to short to look at the first two bytes, you get:
0000001 00000000
which is exactly how it expects to see a value of 1 represented as a two-byte number, so that's what you get.
As implied by the name "little-endian" there are also big-endian systems, where the data would be laid-out as you've illustrated above. On such a machine, you'd probably get the results you expected. Just to be complete, there are also a few systems that use rather strange arrangements that might run something like byte1 byte0 byte3 byte2.
Upvotes: 11
Reputation: 153929
It doesn't point to bytes; it points to an entity of type int
. How an
int is represented in memory depends on the implementation. And if i
is an int
, then *((short*)&i)
is undefined behavior; you could get
anything. In practice, on common platforms, you can access things like
this; what you get still depends on the platform, and such manipulations
are only useful for very low level, platform specific operations. (On an Intel, you'll get the results the others have posted. For other platforms and other values, it depends.)
Upvotes: 3
Reputation: 34626
This has to do with byte order. Your architecture stores an int
1 as:
00000001 00000000 00000000 00000000
^^^^^^^^
this is the address that is stored in the pointer.
|------- -------- -------- --------| int (4 bytes)
|------- -------| short (2 bytes)
which, truncated to a short
of 2 bytes still is a 1.
Btw. try printing all bytes on their own:
unsigned char* p = (unsigned char*)&i;
for (unsigned j = 0; j < sizeof(int); j++) {
std::cout << p[j] << " ";
}
std::cout << std::endl;
It should print 1 0 0 0
, and not 0 0 0 1
as suggested in your question.
Upvotes: 4
Reputation: 4954
It depends on the endianness of your system (x86 is typically little-endian).
In big endian, the value of i will be stored with the most significant byte first :
00000000 00000000 00000000 00000001
In little endian, the value of i will be stored with the least significant byte first :
00000001 00000000 00000000 00000000
In any case, the pointer will point to the beginning of the number. However, since your system is little-endian, casting that pointer to short and dereferencing to get a short value, you will get the first two bytes of the little endian value
00000001 00000000
In little endian, this is the short value for 1, which is the result you get. If you ran the exact same code on a big-endian system, you would then get a value of 0.
Upvotes: 3
Reputation: 36433
That depends on the architecture. And the specific implementation should be completely irrelevant (if it isn't you are doing something wrong).
Upvotes: 3