|
-
June 24th, 2002, 11:09 AM
#1
Why declare struct's with keyword 'typedef'?
Hello,
In other people's source code, I have often seen structures declared in both of the following manners:
struct sample {
...
};
typedef struct sample {
...
};
I do not understand why the keyword 'typedef' is used at times. When I remove the keyword, the programs appear to run in the same manner as when I have the keyword there. If someone could clarify why 'typedef' is placed before structure definitions, that would be helpful. Thanks.
Fierytycoon
-
June 24th, 2002, 11:47 AM
#2
Actually, your second definition is incorrect: it should be
typedef struct
{
...
} name;
This is a hangover from the C days when you had to put struct in front of name if it was a structure. To make life easier, they type defined the struct to be name. In C++, because struct is just class public, there is no need to typedef struct.
Succinct is verbose for terse
-
June 24th, 2002, 01:23 PM
#3
Re: Why declare struct's with keyword 'typedef'?
Originally posted by Fierytycoon
Hello,
In other people's source code, I have often seen structures declared in both of the following manners:
struct sample {
...
};
typedef struct sample {
...
};
I do not understand why the keyword 'typedef' is used at times. When I remove the keyword, the programs appear to run in the same manner as when I have the keyword there. If someone could clarify why 'typedef' is placed before structure definitions, that would be helpful. Thanks.
Fierytycoon
To add to Cup's answer, if these definitions are in a header file, and you intend to use the header in both 'C' and C++ source files, you should leave the "typedef struct" alone. The obvious reason is that the 'C' compiler will throw a fit if you declare your structure the "C++" way 
Regards,
Paul McKenzie
-
June 24th, 2002, 01:46 PM
#4
ah, I see. Thanks to both of you for all of your input.
Fierytycoon
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|