sizeof( _variable_ )

Richard A. O'Keefe ok at quintus.uucp
Mon Jul 25 14:06:20 AEST 1988


In article <6089 at chinet.chi.il.us> les at chinet.chi.il.us (Leslie Mikesell) writes:
>In article <529 at proxftl.UUCP> bill at proxftl.UUCP (T. William Wells) writes:
>>What is happening is this: C defines the size of a type as if it
>>were part of an array.
>
>The point of this is that you can access corresponding elements of successive
>structures in an array by adding sizeof(struct) to a pointer to the
>previous element.

There are two ways of using 'sizeof': 
	sizeof <variable>		-- not using official
and	sizeof (<type>)			-- nonterminals
The connection between sizeof (<type>) and arrays appears to me to mean
that sizeof (<type>) is redundant *IN SOME IMPLEMENTATIONS*:

#define	SIZEOF(Type) ( (char*)&1[(Type *)0] - (char*)&0[(Type *)0] )

This would have worked on the PDP-11, /370, VAX, 3B, &c, and works on Suns.
[The offsetof() macro works much the same way.]
Don't get me wrong: sizeof is a Good Thing because it is much clearer
and more portable than this sort of magic.  The question is whether
sizeof (<type>) was originally foresight or oversight?  Another one
is: in which implementations does SIZEOF not work?



More information about the Comp.lang.c mailing list