Suitably aligned pointers

Dik T. Winter dik at cwi.nl
Sun Oct 23 02:45:40 AEST 1988


In article <8810151524.AA16480 at explorer.dgp.toronto.edu> flaps at dgp.toronto.edu (Alan J Rosenthal) writes:
 > 
 > In article <345 at marob.MASA.COM> daveh at marob.UUCP (Dave Hammond) writes:
 > >Can someone explain the technique used to suitably align a pointer ?
 > 
 > Well, it can't be done very nicely in a portable way.  It can be done very
 > easily in a machine-dependent way:  just do this:
 > 	if ((long)p & 1)
 > 	    p++;
 > for example to enforce even alignment.
It is carefully mentioned that this is machine dependent, but not for what
machines.  This will fail on some.  On at least one machine, to get even
byte alignment, it suffices to do:
	p = (char *)(int *)p;
(Yes, it uses different formats for char* and int*.)
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik at cwi.nl
BITNET/EARN: dik at mcvax



More information about the Comp.lang.c mailing list