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 |
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 |
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. |
To address your first question, what does the following code do?
Code:
int i = 1; 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 Code:
&i Code:
(char *) 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 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 |
It is also important if you want to try to read binary files originally created for another platform.
|
All times are GMT -5. The time now is 09:08 AM. |