Re: Why is typedef needed with struct and enum?
Re: Why is typedef needed with struct and enum?
- Subject: Re: Why is typedef needed with struct and enum?
- From: Charlton Wilbur <email@hidden>
- Date: Fri, 8 Jul 2005 22:46:11 -0400
On Jul 8, 2005, at 10:24 PM, Jerry Krinock wrote:
In CodeWarrior, I've always done the following, which according to
my C/C++
textbook is perfectly good syntax:
struct MyStruct
{
int n1 ;
int n2 ;
} ;
enum NodeType
{
kRoot = 1,
kFolder = 2
} ;
But after porting my code to Xcode, getting errors and then looking at
others' Xcode code, I learned that I now have to do this:
typedef struct MyStruct_type
{
int n1 ;
int n2 ;
} MyStruct ;
typedef enum
{
kRoot = 1,
kFolder = 2
} NodeType ;
Which is OK, according to my book, but why does Xcode make me do
all this
extra typing? I'm afraid that I'm missing an important point.
What CodeWarrior allows you to do is not legal according to the 1989
C standard, and what XCode (really gcc) requires you to do is. It is
legal C++, however. (I don't recall whether it's legal according to
the 1999 C standard.) You say "C/C++" as if they are the same
language, but they aren't, by a long shot, and things that are legal
in one are not legal in another. To make things more complex, there
are two C standards, produced a decade apart, and the gcc compiler
for C adds a host of optional extensions as well.
(In particular, what you must do in C89 is to say this:
struct MyStruct { ... } ;
struct MyStruct instanceOfMyStruct;
or this:
typedef struct { ... } MyStruct;
MyStruct instanceOfMyStruct;
Basically, you can't declare a struct or enum variable without the
struct or enum keyword, or a typedef.)
My suspicion (as the last time I used Codewarrior was in the days of
the PPC 601) is that the Codewarrior compiler is more relaxed about
what it accepts than gcc is, at least as they're both configured out
of the box.
Charlton
--
Charlton Wilbur
email@hidden
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden