"do ... while ((NULL + 1) - 1);" -- valid C?

Ruud Harmsen ruud at targon.UUCP
Wed Aug 16 23:06:57 AEST 1989


In article <18996 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>Consider:  If we write
>	char *cp;
>	int *ip;
>	ip = cp;
>
>the compiler must issue some kind of diagnostic (it says so in the
>proposed ANSI C specification, and it says in K&R-1 that this operation
>is machine-dependent, ...

I suppose this is machine-dependent because of alignment: char-pointers can
point to just about anywhere, but int-pointers on many machines have to be
aligned properly.  My question is: can I make sure in my program, that
though generally non-portable this IS portable?  I tried this once in the
following way:
The char-pointer gets its value from malloc, which the manual says gives
pointers properly aligned for any type.  I never change that char-pointer
other than by adding multiples of sizeof(int) to it.
Is a "ip = cp" guaranteed safe under these conditions, so can I ignore
the compiler-warning?



More information about the Comp.lang.c mailing list