A Deficiency of the C Preprocessor - (nf)

woerz at iaoobelix.UUCP woerz at iaoobelix.UUCP
Mon Dec 22 13:36:00 AEST 1986


> I've got a complaint about the C preprocessor -- I doesn't support a
> repetition for initializing data structures.
>
> We are developing programs where an array of structures exists.
>
>         #define BRKPTCOUNT      5
>         struct .... FooBar[BRKPTCOUNT] = {....};
>
> Everytime we change BRKPTCOUNT, we have to change the initialization data
> for FooBar; if we give extra initialization data, we get complaints from the
> compiler; if we don't give enough data, the rest of the structure isn't
> initialized.

Why don't you write your structures and defines like this:

    #define BRKPTCOUNT  (sizeof (FooBar)/sizeof (FooBar[0]))
    struct ... FooBar [] = { ... } ;

Then your array will be maid as large as needed to fit your data.
The size of the array (defined by BRKPTCOUNT) will be computed during
compile time, since sizeof is a compile time function and the whole
expression is constant.

> ...
------------------------------------------------------------------------------

Dieter Woerz
Fraunhofer Institut fuer Arbeitswirtschaft und Organisation
Holzgartenstrasse 17
D-7000 Stuttgart 1
W-Germany

BITNET: ...unido.bitnet!iaoobel.uucp!woerz
UUCP:   ...seismo!unido!iaoobel!woerz



More information about the Comp.lang.c mailing list