home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19601 < prev    next >
Encoding:
Text File  |  1993-01-12  |  1.5 KB  |  54 lines

  1. Path: sparky!uunet!olivea!gossip.pyramid.com!pyramid!infmx!johnl
  2. From: johnl@informix.com (Jonathan Leffler)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: struct / union / function declaration
  5. Message-ID: <1993Jan12.190650.18172@informix.com>
  6. Date: 12 Jan 93 19:06:50 GMT
  7. References: <1993Jan12.115527.136146@eratu.rz.uni-konstanz.de>
  8. Sender: news@informix.com (Usenet News)
  9. Organization: Informix Software, Inc.
  10. Lines: 42
  11.  
  12. In article <1993Jan12.115527.136146@eratu.rz.uni-konstanz.de>
  13. mueller@DG1.CHEMIE.UNI-KONSTANZ.DE (Christian M"uller) writes:
  14. >I have a problem defining a union like this:
  15. >  
  16. >struct s {
  17. >        union node *arg;
  18. >        union node (*func)(union node);
  19. >};
  20. >
  21. >union node {
  22. >        struct s fun;
  23. >        int n;
  24. >};
  25.  
  26. >A similar problem is the following:
  27. >
  28. >struct x {
  29. >    int (*func)(struct x);
  30. >};
  31.  
  32. Before the struct s declaration, use the line:
  33.  
  34. union node;
  35.  
  36. That is a forward reference which defines the name "node" as a tag for a union.
  37.  
  38. The struct x problem is harder; it would work if you used:
  39.  
  40. struct x {
  41.     int (*func)(struct x *);
  42. };
  43.  
  44. The problem is, before you can pass a structure as a parameter (rather than
  45. a pointer to the structure), you have to know all about it.  You won't know
  46. all about it until you reach the closing brace.
  47.  
  48. And closer scrutiny of struct s reveals the same problem there: you are
  49. trying to pass an incomplete type as a parameter to the function pointer
  50. element, and you can't do it.  Use struture/union pointers instead -- they
  51. are probably more efficient too.
  52.  
  53. My mind is boggled at the thought of trying to use struct x in real life.
  54.