Re: [Q] superfluous ids in self-referential typedef struct

From: Chris Torek (nospam_at_torek.net)
Date: 12/03/04


Date: 3 Dec 2004 21:25:02 GMT

In article <coqb6p$d80$1@reader1.panix.com>
J Krugman <jkrugman345@yahbitoo.com> wrote:
>My compiler complains if I do something like this
>
> typedef struct {
> node *next;
> } node;
>
>but it's OK with
>
> typedef struct superfluous_identifier {
> superfluous_identifier *next;
> } node;

If it is "OK with" this second version, it is not compiling C code
at all. You probably have it set up to compile C++ code -- see
what happens if you write:

    #include <stdio.h>

    struct A { char c[1000]; };

    int main(void) {
        struct B { struct A { char c[1]; } a; char c[1]; };

        printf("sizeof(struct A): %lu\n", (unsigned long)sizeof(struct A));
        return 0;
    }

This program is legal in both languages, but is almost certain to
produce different results:

    % ln t.c t.c++
    % cc -O -o t t.c && ./t
    sizeof(struct A): 1
    % g++ -O -o t t.c++ && ./t
    sizeof(struct A): 1000
    %

>I hate that superfluous_identifier. Is there any way to avoid it?

Stop using typedef. :-)

Seriously, typedef does not really buy you anything here. (Remember,
typedef does not define new types. "struct" defines new types.)
Adding the superfluous typedef-identifier allows you to stop typing
out the keyword "struct". Is it really that hard to type the word
"struct", and a space, each time? Why is:

    typedef struct node node;
    struct node {
        node *next;
        /* contents here */
    };
    node *head;

so much more appealing to some people than:

    struct node {
        struct node *next;
        /* contents here */
    };
    struct node *head;

? (I prefer the second version myself. I *like* that "struct"
keyword. It is like a cute little kitten -- who could possibly
hate it? :-) )

-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Relevant Pages

  • Re: [PATCH 1/1] LinuxPPS: Pulse per Second support for Linux
    ... Follows my comments and then the patch, hope now I can came back into ... +typedef union pps_timeu { ... +typedef struct pps_info { ... different version of the compiler, and when different compiler options are ...
    (Linux-Kernel)
  • Re: Process dump facility public API - pdpublic.h
    ... struct _PDOPTIONS *pSystemDefaults; /* Ptr to System Defaults struct */ ... typedef DDPREQUEST *PDDREQUEST; ... also be specified in the DDPREQUEST flags. ... /* PDUNION is used in both the PDPROCESS and PDPROCESS2 structures. ...
    (comp.os.os2.bugs)
  • Re: advantage of using typedefs
    ... The underlying problem / solution here is "abstraction". ... struct, ... possible to call this with a length or pressure measurement by ... typedef double temperature; ...
    (comp.lang.c)
  • Re: C90 penetration
    ... If we agree on a set of digits that includes all Latin 1 alpha, ... if 'double' were indeed a typedef for a struct ... And there are a number of other reasons why making "double" a typedef ... for a struct type would make a compiler non-conforming. ...
    (comp.lang.c)
  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)