Re: [Q] superfluous ids in self-referential typedef struct
From: Chris Torek (nospam_at_torek.net)
Date: 12/03/04
- Next message: Eric Sosman: "Re: ptrdiff_t"
- Previous message: Keith Thompson: "Re: Exception trapping in C?"
- In reply to: J Krugman: "[Q] superfluous ids in self-referential typedef struct"
- Next in thread: Chris Croughton: "Re: [Q] superfluous ids in self-referential typedef struct"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 3 Dec 2004 21:25:02 GMT
In article <coqb6p$d80$1@reader1.panix.com>
J Krugman <jkrugman345@yahbitoo.com> wrote:
>My compiler complains if I do something like this
>
> typedef struct {
> node *next;
> } node;
>
>but it's OK with
>
> typedef struct superfluous_identifier {
> superfluous_identifier *next;
> } node;
If it is "OK with" this second version, it is not compiling C code
at all. You probably have it set up to compile C++ code -- see
what happens if you write:
#include <stdio.h>
struct A { char c[1000]; };
int main(void) {
struct B { struct A { char c[1]; } a; char c[1]; };
printf("sizeof(struct A): %lu\n", (unsigned long)sizeof(struct A));
return 0;
}
This program is legal in both languages, but is almost certain to
produce different results:
% ln t.c t.c++
% cc -O -o t t.c && ./t
sizeof(struct A): 1
% g++ -O -o t t.c++ && ./t
sizeof(struct A): 1000
%
>I hate that superfluous_identifier. Is there any way to avoid it?
Stop using typedef. :-)
Seriously, typedef does not really buy you anything here. (Remember,
typedef does not define new types. "struct" defines new types.)
Adding the superfluous typedef-identifier allows you to stop typing
out the keyword "struct". Is it really that hard to type the word
"struct", and a space, each time? Why is:
typedef struct node node;
struct node {
node *next;
/* contents here */
};
node *head;
so much more appealing to some people than:
struct node {
struct node *next;
/* contents here */
};
struct node *head;
? (I prefer the second version myself. I *like* that "struct"
keyword. It is like a cute little kitten -- who could possibly
hate it? :-) )
-- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 email: forget about it http://web.torek.net/torek/index.html Reading email is like searching for food in the garbage, thanks to spammers.
- Next message: Eric Sosman: "Re: ptrdiff_t"
- Previous message: Keith Thompson: "Re: Exception trapping in C?"
- In reply to: J Krugman: "[Q] superfluous ids in self-referential typedef struct"
- Next in thread: Chris Croughton: "Re: [Q] superfluous ids in self-referential typedef struct"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|