Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: rms from rosettacode |
| Date | 2011-12-28 13:47 -0800 |
| Organization | None to speak of |
| Message-ID | <lnipl0z7uq.fsf@nuthaus.mib.org> (permalink) |
| References | (1 earlier) <20111227115244.994@kylheku.com> <4efa2b39$0$1383$4fafbaef@reader2.news.tin.it> <jddska$550$1@dont-email.me> <jdf43c$aph$1@dont-email.me> <jdf5sd$kd8$1@dont-email.me> |
"BartC" <bc@freeuk.com> writes:
[...]
> For example, I had trouble the other night with creating a struct with
> self-referential members. I tried loads of combinations including forward
> declarations and got nowhere. I was about to post here, then came across
> this form in some code that was lying around:
>
> typedef struct snode{
> int value;
> int opcode;
> struct snode *aref,*bref;
> } node;
>
> That worked, but how can I turn the 'struct snode*' into a much tidier 'node
> *'? This doesn't work:
>
> typedef struct {
> int value;
> int opcode;
> node *aref,*bref;
> } node;
My suggestion is just to drop the "typedef" altogether, and refer to the
type as "struct snode":
struct snode {
int value;
int opcode;
struct snode *aref;
struct snode *bref;
};
If you feel the need to have a one-word name for the type, you can
typedef the incomplete struct type and then complete it by defining it.
Note that there's no need to use different names for the struct tag and
the typedef:
typedef struct snode snode;
struct snode {
int opcode;
snode *aref;
snode *bref;
};
Another common approach is to combine the typedef and struct
declarations -- but then the typedef isn't visible until the end
of the struct declaration:
typedef struct snode {
int opcode;
struct snode *aref;
struct snode *bref;
} snode;
But really, the type already has a perfectly good name, "struct snode".
Hiding its "structness" behind a typedef doesn't buy you anything.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 11:25 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 06:48 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 13:01 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 07:10 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 13:36 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 10:04 -0500
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 14:42 -0800
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 18:46 -0500
Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-27 09:49 -0800
Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-27 18:14 +0000
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 19:33 +0100
Re: rms from rosettacode jt@toerring.de (Jens Thoms Toerring) - 2011-12-27 19:04 +0000
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 14:45 -0800
Re: rms from rosettacode blp@cs.stanford.edu (Ben Pfaff) - 2011-12-27 14:57 -0800
Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-27 19:06 +0000
Re: rms from rosettacode Bull in a China Blue Shop <chine.bleu@yahoo.com> - 2011-12-27 16:16 -0800
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-27 14:11 -0500
Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-27 20:13 +0000
Re: rms from rosettacode Johann Klammer <klammerr@NOSPAM.aon.at> - 2011-12-28 00:23 +0100
Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-27 15:54 -0800
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-27 22:53 -0800
Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-28 06:29 +0000
Re: rms from rosettacode 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-28 02:45 -0800
Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-27 19:53 +0000
Re: rms from rosettacode Seebs <usenet-nospam@seebs.net> - 2011-12-27 20:13 +0000
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-27 21:31 +0100
Re: rms from rosettacode Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-27 20:49 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-28 11:42 +0100
Re: rms from rosettacode Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-29 08:18 -0800
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-28 11:54 +0100
Re: rms from rosettacode Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-29 04:12 +0200
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-29 14:38 +0100
Re: rms from rosettacode Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:26 -0800
Re: rms from rosettacode Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-28 21:35 -0500
Re: rms from rosettacode superpollo <superpollo@tznvy.pbz> - 2011-12-29 14:10 +0100
Re: rms from rosettacode James Kuyper <jameskuyper@verizon.net> - 2011-12-28 08:03 -0500
Re: rms from rosettacode "BartC" <bc@freeuk.com> - 2011-12-28 13:32 +0000
Re: rms from rosettacode Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-28 13:41 +0000
Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-28 19:29 +0000
Re: rms from rosettacode Keith Thompson <kst-u@mib.org> - 2011-12-28 13:47 -0800
Re: rms from rosettacode Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-29 04:19 +0200
Re: rms from rosettacode Kaz Kylheku <kaz@kylheku.com> - 2011-12-28 19:11 +0000
csiph-web