home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!gossip.pyramid.com!pyramid!infmx!johnl
- From: johnl@informix.com (Jonathan Leffler)
- Newsgroups: comp.lang.c
- Subject: Re: struct / union / function declaration
- Message-ID: <1993Jan12.190650.18172@informix.com>
- Date: 12 Jan 93 19:06:50 GMT
- References: <1993Jan12.115527.136146@eratu.rz.uni-konstanz.de>
- Sender: news@informix.com (Usenet News)
- Organization: Informix Software, Inc.
- Lines: 42
-
- In article <1993Jan12.115527.136146@eratu.rz.uni-konstanz.de>
- mueller@DG1.CHEMIE.UNI-KONSTANZ.DE (Christian M"uller) writes:
- >I have a problem defining a union like this:
- >
- >struct s {
- > union node *arg;
- > union node (*func)(union node);
- >};
- >
- >union node {
- > struct s fun;
- > int n;
- >};
-
- >A similar problem is the following:
- >
- >struct x {
- > int (*func)(struct x);
- >};
-
- Before the struct s declaration, use the line:
-
- union node;
-
- That is a forward reference which defines the name "node" as a tag for a union.
-
- The struct x problem is harder; it would work if you used:
-
- struct x {
- int (*func)(struct x *);
- };
-
- The problem is, before you can pass a structure as a parameter (rather than
- a pointer to the structure), you have to know all about it. You won't know
- all about it until you reach the closing brace.
-
- And closer scrutiny of struct s reveals the same problem there: you are
- trying to pass an incomplete type as a parameter to the function pointer
- element, and you can't do it. Use struture/union pointers instead -- they
- are probably more efficient too.
-
- My mind is boggled at the thought of trying to use struct x in real life.
-