LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2005, 04:15 PM   #1
KissDaFeetOfSean
LQ Newbie
 
Registered: Jun 2005
Posts: 27

Rep: Reputation: 15
Pointer Conversions


Hi,
I am playing with the following C program:

/* 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?

Sean
 
Old 07-19-2005, 05:59 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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.
 
Old 07-19-2005, 06:15 PM   #3
KissDaFeetOfSean
LQ Newbie
 
Registered: Jun 2005
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks, I understand now.
 
Old 07-21-2005, 02:17 AM   #4
kite10linux
Member
 
Registered: Jul 2005
Posts: 51

Rep: Reputation: 15
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
XViD to MPEG-2 conversions? r_jensen11 Linux - Software 9 04-19-2004 06:59 AM
IDN conversions markus1982 Programming 1 03-11-2004 12:38 PM
Bulk image format conversions with a shell script? infidel Linux - Software 4 11-16-2003 07:20 PM
Need help with conversions in C green_dragon37 Programming 1 06-07-2003 01:03 AM
Graphics conversions that_girl Linux - Newbie 1 04-05-2002 07:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration