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
Re: Why declare struct's with keyword 'typedef'?
Quote:
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