2
#include<stdio.h>
int main()                                                                                                                              
{
    unsigned int unum = 0x80008001;
    short unsigned int *snum = (short unsigned int*)&unum;
    printf("%d\n", *snum);
    printf("%d\n", *(snum+1));
    return 0;
}

Output:

32769
32768

snum is pointing to the initial two bytes of the unum, but it's value is last two bytes of unum, and after incrementing pointer, value is the first two byte of unum.

So, It is a little endian system. But, i am not sure whether this way is right or not to know the system's type. Is it proper?

Jagdish
  • 1,848
  • 3
  • 22
  • 33
  • 1
    C++ answer: Or it won't compile, perhaps? – Mats Petersson Oct 11 '14 at 12:44
  • 3
    Obvious strict aliasing violation is obvious. – T.C. Oct 11 '14 at 12:45
  • @HotLicks: Not how I read it. The first number is the low part, right? – Mats Petersson Oct 11 '14 at 12:45
  • @MatsPetersson - Yeah, it got me confused. – Hot Licks Oct 11 '14 at 12:46
  • possible duplicate of [How to check whether a system is big endian or little endian?](http://stackoverflow.com/questions/4181951/how-to-check-whether-a-system-is-big-endian-or-little-endian) – 2501 Oct 11 '14 at 13:09
  • @2501 ,I am asking whether this is the proper way or not, i have also seen the similar questions on SO. – Jagdish Oct 11 '14 at 13:19
  • @jagsgediya: Given that as C++ it doesn't compile at all, and as C will give you warnings, I'd say that's not a good sign. Don't you think? – Mats Petersson Oct 11 '14 at 13:21
  • @MatsPetersson **gcc code.c -Wall -Werror**, not showing any error or warnings. – Jagdish Oct 11 '14 at 13:24
  • Ah, because you fixed the missing cast. I would still prefer to use a `char` than an `unsigned short`, simply because it will ensure that you don't break strict aliasing rules. – Mats Petersson Oct 11 '14 at 13:32
  • Which standard are you reading, and where. AS Loopunroller says, the C standard forbids casting a pointer to another type. You could use `memcpy` or use `char` or `unsigned char`, since those are allowed to be cast to/from by the language. – Mats Petersson Oct 11 '14 at 13:55

2 Answers2

5

Your code induces undefined behavior as you violate the strict aliasing rule (§6.5/7 for the C standard, [basic.lval]/10 for C++). The code accesses the stored value of unum through an lvalue of a completely different type. With a C++ compiler the above code won't even compile since implicit casts between unrelated pointer types are forbidden.

Use char or unsigned char aliases to access single bytes from an object representation, those types are excluded from the strict aliasing rule. Here are examples.

Community
  • 1
  • 1
Columbo
  • 60,038
  • 8
  • 155
  • 203
1

For me the code you've submitted does not compile, thus I would assume you cannot do this trick. However this post shows how to solve the problem you're talking about.

My compiler:

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Community
  • 1
  • 1
lisu
  • 2,213
  • 14
  • 22