by cjensen · 10y ago · 0 comments
(a) char may be signed or unsigned so
char a, b;
...
short c = (short (a) << 8 | b);
will not work to construct a 16-bit value from two 8-bit values.(b) NULL is not a useful macro. For example,
printf ("%p", NULL);
can have unexpected results.Those things mentioned in the article? Outside of DSPs and GPUs, you can ignore them.
(((short)((unsigned char)a)) << 8)
Which works but goddamn is it ever hard to read. Bracket highlighting makes it just bearable. Fortunately I've abstracted this kind of stuff out as much as possible so I only have it in a few places, but it still bites me in the ass sometimes. Curious if anyone has a cleaner way of expressing this?How do you figure?
#define NULL ((void *) 0)
or just plain #define NULL 0
The latter works correctly because a literal 0 in pointer context is interpreted by the compiler as a null pointer.If it is 0, the printf() provides no pointer context, so the 0 is interpreted as an int. That works... poorly if sizeof (int) < sizeof (void *)
Is there any reason for an implementation to elect to use the non-casted version of NULL? It seems like an oversight.