sizeof( _variable_ )

Leslie Mikesell les at chinet.chi.il.us
Mon Jul 25 06:24:25 AEST 1988


In article <529 at proxftl.UUCP> bill at proxftl.UUCP (T. William Wells) writes:
>:.....  I should think that sizeof( _variable_ )
>: should be the length of the _variable_ not the length of the memory allocated
>: to it.

>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.  If sizeof(struct) did not include the padding there
would be no way to find the next corresponding element from the address
of the previous, and loops accessing one element of each struct in an
array would have to be written in a less efficient way (and you might have
to deal with pointers to structs and the necessity of casting them for
most operations instead of using pointers to the data type of the element
you are accessing).

  Les Mikesell



More information about the Comp.lang.c mailing list