LinuxQuestions.org
Help answer threads with 0 replies.
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 11-28-2005, 03:55 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Rep: Reputation: 45
explain me some c code


Can u plz explain me what this code does?


int i = 1;
char *p = (char *) &i;

I have found it in a piece of paper and i am trying to understand what it means
 
Old 11-28-2005, 04:03 AM   #2
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
Hmm after rethinking it i have ask more
int i = 1;
char *p = (char *) &i;
if (p[0] == 1) // Lowest address contains the least significant byte
return LITTLE_ENDIAN;
else
return BIG_ENDIAN;
check the code above ...
the p[0] i think it stores character so i think that p[0]==1 is wrong and we have to use strcmp instead..
thx a lot
 
Old 11-28-2005, 04:42 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
I think the code is probably right.

In the code it creates an "int" which is an integer which is 2/4/8 bytes long depending on the implementation (but that doesn't matter). It assigns the integer 1, which consists of 0x0001 or something like that (i.e. a 1 in the least significant bit and all 0s otherwise). Then it tries to interpret the memory location as "char", which is an integer of 1 byte long; so that p[0] represents the first byte of the several bytes that the integer is stored in.

If the machine is little-endian, then that is the byte where the least significant bits are stored, which will be a bunch of 0s and a 1, so that the value of that byte is 1. If the machine is big-endian, then the least significant bits are stored in the last byte, and all the other bytes (including p[0]) will be 0.
 
Old 11-28-2005, 09:13 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
To address your first question, what does the following code do?
Code:
 int i = 1;
 char *p = (char *) &i;
The first line declares an integer and assigns one into it.
The second line says give me a pointer to a character and point to the address of the integer declared in line one. This can be broken down as follows:
Code:
char *p
is the definition of the character pointer
Code:
&i
is the address of the integer i
Code:
(char *)
is a type cast from integer pointer to character pointer

You second question identifies why this may be necessary:
An integer will consist of at least two bytes (16 Bits) Different operating systems store integers differently, in what is known as Big endian or little endian. This indicates the order in which the bytes are stored internally, as a programmer you don't need to worry about it because the compiler will always manage the storage of an integer in a consistent way. Big endian stores the most significant bytes in the left most position whilst little endian stores the least significant bytes in the left most position.

So where does the character pointer come in? This provides a useful overlay on the storage of an integer

Code:
Big Endian    00000000 00000001 most significant byte stored on the left
Little Endian 00000001 00000000 least significant byte stored on the left
              |______| |______|
char pointer     [0]      [1]
By treating the character pointer as an array of characters the first element will look at the left most byte of the integer and it is then possible to determine if the integer is stored internally as a Big endian or a little endian number.

So after all that why may we want to know how a number is stored?
I said that as a programmer we don't need to know, I lied It is important when we connect computers on a network, because they may be running different O/S the way they store numbers internally (such as an IP address or a port number) may differ and so it is important to ensure that on a network they 'speak' the same language, this is often referred to as network byte order.

As an example take a Windows machine (which uses Big endian) and Linux machine (which uses Little endian); if a two byte number 1 is sent from one machine to the other without any regard on the byte order then the receiving machine will not see the number one but because the byte order is switched from what it expect it will see the number 256.

Sorry for being so long, but I hope that helps.

graeme
 
Old 11-28-2005, 12:27 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
It is also important if you want to try to read binary files originally created for another platform.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 03:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 02:37 AM
explain me this c code plz alaios Programming 1 06-03-2005 06:32 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 10:32 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 12:16 PM


All times are GMT -5. The time now is 02:36 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration