Keith said:
To determine if a machine is little endian / big endian the [following] code
snippet is used...
int num = 1;
if( * (char *)&num == 1)
printf ("\n Little Endian");
else
printf("\n Big endian");
That will probably work, but it's not guaranteed. And before you use
it, you should think about why you care whether your system is
little-endian or big-endian. There are cases where you need to know,
but I think there are more cases where people *think* they need to know
but really don't.
For networking you need to know whether to swop the incoming and outgoing
byte order...
this is *the* example of when it matters. If its going out of the
machine it'll an external representation and then endianess etc.
matters. The other example is a binary file. (Actually *any* file- but
we seem to think that for text files it is easier to agree on a
representation (XML...)).
Yes, this is dumb. A void* and char* are exactly the same under the hood.
but they differ in their semantics (meaning).
A void* you can convert without a typecast but you can't dereference. A
char* you can dereference but not convert automatically. Why not have one
pointer type with both features??
because they mean different things. GCC allows dereferencing a null
pointer as an extension, but I think GCC is broken!
the memory is undisturbed.
But what I mean is that the int's memory is being regarded as a char,
isn't it?
not really...
don't some of the toupper() thingies need casts to gurantee correct
operation?
It would make life simpler if all typecasts were either disallowed or
guaranteed portable!
I don't think you really understand what casts are for. In many cases
you are using a cast *because* you want non-portable results. Eg. to
discover the endianess of your system...
--
Casting is almost always wrong,
and the places where it's right are rarely the places you'd guess.
Richard Heathfield
Abuse of casting leads to abuse of the type system
leads to sloppy programming leads to
unreliably, even undefined, behaviour.
And that is the path to the dark side....
Richard Bos/John Hascall
We recommend, rather, that users take advantage of the extensions of
GNU C and disregard the limitations of other compilers. Aside from
certain supercomputers and obsolete small machines, there is less
and less reason ever to use any other C compiler other than for
bootstrapping GNU CC.
(Using and Porting GNU CC)