Click to See Complete Forum and Search --> : Why declare struct's with keyword 'typedef'?
Fierytycoon
June 24th, 2002, 11:09 AM
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
cup
June 24th, 2002, 11:47 AM
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.
Paul McKenzie
June 24th, 2002, 01:23 PM
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
Fierytycoon
June 24th, 2002, 01:46 PM
ah, I see. Thanks to both of you for all of your input.
Fierytycoon
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.