Alex Rivera | Logout

Why should structure names have a typedef?

Asked 2010-04-09T07:15:04.307
9

I have seen source codes always having a typedef for a structure and using the same everywhere instead of using the structure name as "struct sname" etc directly?

What is the reason behind this? Are there any advantages in doing this?

Edit
Report

1 Answer

4

It is an odd quirk in the C language, structure tag names are stored in a different namespace (symbol table). The C++ language got rid of this behavior. The typedef creates an alias for the structure type in the global namespace. So you don't have to type "struct" before the structure name.

Not sure what Ritchie was smoking when he defined this behavior. I'm guessing at some kind of problem with the parser in an early version of the compiler.

answered 2010-04-09T10:42:20.580

Your Answer