home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19569 < prev    next >
Encoding:
Internet Message Format  |  1993-01-12  |  1.8 KB

  1. Path: sparky!uunet!portal!cup.portal.com!Aurelius
  2. From: Aurelius@cup.portal.com (Mark Christian Barnes)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: struct vs. typedef
  5. Message-ID: <73413@cup.portal.com>
  6. Date: Mon, 11 Jan 93 23:09:01 PST
  7. Organization: The Portal System (TM)
  8. Distribution: na
  9. References: <uRL4wB7w165w@cybrspc.uucp>
  10.   <JAR.93Jan10152829@solva.ifi.uio.no> <1993Jan11.170937.22822@informix.com>
  11. Lines: 56
  12.  
  13. johnl@informix.com (Jonathan Leffler) writes:
  14.  
  15. |In article <JAR.93Jan10152829@solva.ifi.uio.no>
  16. | jar@solva.ifi.uio.no (Jo Are Rosland) writes:
  17. |>In article <uRL4wB7w165w@cybrspc.uucp> Roy M. Silvernail writes:
  18. |>
  19. |>   typedef struct n {
  20. |>    int x,y;
  21. |>    struct n *last,*next, *up, *down;
  22. |>    void (*show)(struct n *);
  23. |>    int width;
  24. |>    void **value;
  25. |>   } ITEM;
  26. |>
  27. |>   Now, this works, but it sure seems clumsy.  Is this really the only way
  28. |>   to do this type of construct?  If not, is it the optimal solution, or is
  29. |>   there some extremely clever trick I'm missing?
  30. |>
  31. |>Given C, that's the best you can get.  You'll just have to get used to
  32. |>it, just like the rest of us :-).
  33. |
  34. |Not so:
  35. |
  36. |typedef struct n ITEM;
  37. |struct n {
  38. |       int x,y;
  39. |       ITEM *last,*next, *up, *down;
  40. |       void (*show)(ITEM *);
  41. |       int width;
  42. |       void **value;
  43. |};  
  44. |I prefer the style:
  45. |typedef struct Tag Tag;
  46. |struct Tag
  47. |{
  48. |        ...
  49. |};
  50. |where the structure tag and the typdef name are the same -- it gets closer to
  51. |C++ that way.  
  52.  
  53.  Yes I declare structures and unions nearly this way myself. My
  54. preference is to keep the declaration atomic though...
  55.  
  56. typedef struct TagS        /* declare type TagS */
  57. {
  58.  struct TagS *head;
  59.  struct TagS *tail;
  60. } TagS;
  61.  
  62. TagS aTag, *pTag = &aTag;    /* declare some variables */
  63.  
  64. This requires that any recursive structure references say "struct",
  65. but everywhere else the typedef name is enough.
  66.  
  67.             Regards, Aurelius@cup.portal.com
  68.