home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!portal!cup.portal.com!Aurelius
- From: Aurelius@cup.portal.com (Mark Christian Barnes)
- Newsgroups: comp.lang.c
- Subject: Re: struct vs. typedef
- Message-ID: <73413@cup.portal.com>
- Date: Mon, 11 Jan 93 23:09:01 PST
- Organization: The Portal System (TM)
- Distribution: na
- References: <uRL4wB7w165w@cybrspc.uucp>
- <JAR.93Jan10152829@solva.ifi.uio.no> <1993Jan11.170937.22822@informix.com>
- Lines: 56
-
- johnl@informix.com (Jonathan Leffler) writes:
-
- |In article <JAR.93Jan10152829@solva.ifi.uio.no>
- | jar@solva.ifi.uio.no (Jo Are Rosland) writes:
- |>In article <uRL4wB7w165w@cybrspc.uucp> Roy M. Silvernail writes:
- |>
- |> typedef struct n {
- |> int x,y;
- |> struct n *last,*next, *up, *down;
- |> void (*show)(struct n *);
- |> int width;
- |> void **value;
- |> } ITEM;
- |>
- |> Now, this works, but it sure seems clumsy. Is this really the only way
- |> to do this type of construct? If not, is it the optimal solution, or is
- |> there some extremely clever trick I'm missing?
- |>
- |>Given C, that's the best you can get. You'll just have to get used to
- |>it, just like the rest of us :-).
- |
- |Not so:
- |
- |typedef struct n ITEM;
- |struct n {
- | int x,y;
- | ITEM *last,*next, *up, *down;
- | void (*show)(ITEM *);
- | int width;
- | void **value;
- |};
- |I prefer the style:
- |
- |typedef struct Tag Tag;
- |struct Tag
- |{
- | ...
- |};
- |where the structure tag and the typdef name are the same -- it gets closer to
- |C++ that way.
-
- Yes I declare structures and unions nearly this way myself. My
- preference is to keep the declaration atomic though...
-
- typedef struct TagS /* declare type TagS */
- {
- struct TagS *head;
- struct TagS *tail;
- } TagS;
-
- TagS aTag, *pTag = &aTag; /* declare some variables */
-
- This requires that any recursive structure references say "struct",
- but everywhere else the typedef name is enough.
-
- Regards, Aurelius@cup.portal.com
-