Finding Endianess

Hi

I came across a code to find the endianess, wherein a union containing an integer and character is initialized and the integer is set to 1. Now, say that this is big endian, the MSB is going to be zero. Now what would be the value of the character? Would it be '0' or some other format ?
Sounds more complicated than necessary.
1
2
3
4
5
int a=1;
if (*(unsigned char *)&a==1)
    //little
else
    //big 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

union End
{
        unsigned        uiValue;
        unsigned char   cValue[sizeof(unsigned)];
};

int main()
{
        End e;
        e.uiValue = 1;

        std::cout << "size is: " << sizeof(e) << std::endl;
        std::cout << "e.uiValue = " << e.uiValue << std::endl;

        for (size_t i = 0; i != sizeof(e.cValue); ++i)
                std::cout << "cValue[" << i << "]=" << (int)e.cValue[i] << std::endl;

        return 0;
}


Executed on an Intel Mac:
1
2
3
4
5
6
size is: 4
e.uiValue = 1
cValue[0]=1
cValue[1]=0
cValue[2]=0
cValue[3]=0


Executed on a PowerPC Mac:
1
2
3
4
5
6
size is: 4
e.uiValue = 1
cValue[0]=0
cValue[1]=0
cValue[2]=0
cValue[3]=1

This made me thinking: if there is a C-style cast like in "*(unsigned char *)&a", how can it be known what exactly in an arbitrary expression will be tried to cast? Do you have any real-life ambiguities in mind?
how can it be known what exactly in an arbitrary expression will be tried to cast? Do you have any real-life ambiguities in mind?
The first three times I read these sentences, I thought you were talking about the undefined behavior of the expression. C Casts have, like all other operators, precedence. If you know the precedence rules, you can unambiguously predict the order of evaluation.

http://cs.smu.ca/~porter/csc/ref/cpp_operators.html (The server may be down. Too bad, because it's a very useful link.)
Well it's down. If I look into [1], I see (type) and now I understand: it's all the same precedence, but associativity is right-to-left... I missed to see (type).

[1] http://www.cppreference.com/wiki/operator_precedence
@helios: Yes thats another way to do it. But my doubt is how can you equate a dereferenced char pointer (even though it has been typecasted from int pointer) to 1. Shouldn't it be more like a character equivalent of the integer 1.

So my question essentially boils down to this: If i have say an int pointer and typecast it to say, a different pointer type, when i dereference it, will I still get an integer ?

But my doubt is how can you equate a dereferenced char pointer (even though it has been typecasted from int pointer) to 1. Shouldn't it be more like a character equivalent of the integer 1.
The only difference between char and int is that char is smaller. '1' just means "the integer value of type char that this machine understand as the character for the Arabic numeral '1'". In other words (assuming ASCII), '1' and char(49) are semantically identical.

If i have say an int pointer and typecast it to say, a different pointer type, when i dereference it, will I still get an integer ?
No. You'll get what the pointer type you casted to is supposed to be pointing to. If you casted to char *, you dereference into a char; if you casted to std::vector<std::list<std::basic_string<unsigned long long> > > *, you dereference into a std::vector<std::list<std::basic_string<unsigned long long> > >.
Of course, there's no telling what will happen if what was originally at that memory location and what you're casting to are incompatible. For example,
1
2
3
std::vector<int> v(10,20);
std::list<int> *l=(std::list<int> *)&v;
l->push_back(30); //??? 

Last edited on
Yes. Thats what I expected. So, in

if (*(unsigned char *)&a==1)

what I am trying to say is that the left hand side of the equation represents a char type and the right hand represents an integer type. That is where I am confused....
helios wrote:
The only difference between char and int is that char is smaller.
chars are integers, as well.
Last edited on
Topic archived. No new replies allowed.