Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

why are typedefs used in conjunction with structs in C?

OpenStudy (anonymous):

for example: typedef struct { char[30] name; unsigned long score; } SCORE;

OpenStudy (farmdawgnation):

It allows you to declare instances of that struct without using the struct keyword for every declaration. So, that code allows you to type "SCORE theScore;" and it would be fully declared. More on this behavior can be found here: http://en.wikipedia.org/wiki/Typedef#Simplifying_a_declaration

OpenStudy (asnaseer):

The main purpose of typedef is to abstract away the actual type details from the code that uses it. if you declared the type as: struct ScoreStruct {...}; then users of this would declare variables of this type as: struct ScoreStruct myScrore; this couples the details of the struct with the code that is trying to use it. On the otherhand, if you abstracted away the type using the typedef you declared above, then the user would simply declare their variable as: SCORE myScore; now lets say later in the life of your program you decide that using a class instead of a struct would be better. because you used typedef, the users of your data type do not need to change their declarations, the only change would be in your code where you would have something like this now: class Score {...}; typedef Score SCORE;

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!