ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
/* convert.c: char * and pointer casts */
#include <stdio.h>
int main (int argc, char * argv[]) {
int n;
int i = 7;
char * cp = (char *) &i;
printf ("The integer at %p == %04X\n", &i, i);
for (n = 0; n < sizeof i; ++n)
printf ("The byte at %p == %02X\n", cp + n , *(cp + n));
return 0;
}
which produces the following output on my machine, using VisC++:
The integer at 0012FEC8 == 0007
The byte at 0012FEC8 == 07
The byte at 0012FEC9 == 00
The byte at 0012FECA == 00
The byte at 0012FECB == 00
Press any key to continue
My question is if "i" is at address 0x0012FEC8, then I understand why the first time in the loop it prints "The byte at 0012FEC8 == 07"; but if we keep incrementing that address by one (which cp holds/points to) then why are we getting "00" for the next 3 outputs. I think it's because we're visiting each byte of a 4 byte int one by one and printing it out. But why are the least significant bits stored in the first (leftmost position)? I did some more reading and hunting, and it seems to have to do with this business about Big Endian vs. Little Endian architecture. Could someone please explain?
I really don't get what the question is. As far as I see you got all the answers found by yourself.
The intel processors and all the PC processors use Little Endian byte order, which means that the less significant byte is stored in the lowest address. This has to do with the bytes and not the bits.
In both Little Endian and Big Endian, the 8 bits in a byte are stored in the same way.
But if a variable has a size greater than 1 byte, it is split into bytes which are stored according to the byte order used by that particular architecture.
If you had been on a Big Endian machine, then your output might have looked something like:
byte at 0x001 = 00
byte at 0x002 = 00
byte at 0x003 = 00
byte at 0x004 = 07
In some cases, you can set either Big Endian or Little Endian as a configuration parameter. But, you probably did not have to worry about that. The Big Endian processor / machine would store the most significant byte at the lowest address. The bytes are ordered by significance by the place that they are at:
for the hexadecimal (base 16) number 0x12345678, the most significant byte is "12" while the least significant byte is "78". For the above example, on a PC (Little Endian):
byte at 0x001 = 78
byte at 0x002 = 56
byte at 0x003 = 34
byte at 0x004 = 12
While on a Big Endian machine it would be:
byte at 0x001 = 12
byte at 0x002 = 34
byte at 0x003 = 56
byte at 0x004 = 78
It would be instructive for you to set the interger eqaul to 0x12345678 to see what happens! The interger value of 7 is not unique enough to see what is happening. Give it a try!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.