What do *YOU* think of typedefs? (A long message)

Rick Genter reg at vaxine.UUCP
Thu Mar 1 13:23:33 AEST 1984


<This line intentionally left non-blank.>

     In the past there have been various coding style discussions (arguments?
abuse?) regarding features such as indentation, where to put the braces, etc.
I now wish to bring up an issue which I have not seen raised before, but find
to be fairly important.

     What is the opinion of the net community regarding the use of typedefs?  I
am currently working on a project involving a *very* large software system
(roughly 100,000 lines of C code).  Most of the types have been typedef'd to
more meaningful names.  This is fine except for one specific case:

        typedefING ARRAY TYPES SUCKS.

     Perhaps I should explain.  There are places in the software system I am
working on which want to use a data structure as an array (mostly looping over
the elements on the array to perform some operation on all elements of the
array).  There are also places which want to use the same data structured as a
struct (compute some equation using the 2nd and 5th element of a 6 element
array).

     When a typedef represents a struct, I don't have much trouble.  For
example, if I have:

        typedef struct  {
                float   thing1;
                float   thing2;
                float   thing3;
                float   thing4;
                float   thing5;
                float   thing6;
                }       Things;

then it's nice to say:

        Things  group;

        group.thing3 = group.thing5 - group.thing4;

        Manipulate (& group)

        Manipulate (g)
        Things  *g;

and so on.  Now look what happens when I have:

        typedef float   Things[ 6 ];

        Things  group;

        group[ 3 ] = group[ 5 ] - group[ 4 ];

        Manipulate (group)      <- If I put the &, I get a warning from cc

        Manipulate (g)
        Things  g;              <- If I put the *, I get something totally
                                   different that what I want!

     I guess the problem boils down to the fact that arrays are treated
differently than any other type in the language.  I have heard complaints
registered against C since structures became lvalues, but at least structs end
up getting treated more like all other types, such as:

        If I want the address of the struct, I say & struct;
        If I want a pointer to a struct, I say * struct;
        If I want to pass a struct, I say routine(struct);

     Getting back to my original question, what are the attitudes of other
hackers towards typedefs?  After hacking at a lot of the code in this software
system (about 1/3 of it) I am finding that I would be much better off (and the
code would be much more readable) if the structs are typedefd but the arrays
aren't.  Are there any other reasonable alternatives?

                                        Rick Genter
                                        Automatix Inc.
                                        ...!linus!vaxine!reg



More information about the Comp.lang.c mailing list