Coder's Guild Mailing List

Re: typedef struct vs struct

Posted by Fox on 2000-02-06

> typedef struct tagMSG {
>     ....
>     ....
> } MSG;
> 

This code enables you to do this when defining a object of that type:
MSG message;

> and the other one is:
> 
> struct MSG {
>     ...
>     ...
> };

This makes you do this when defining an object of that struct:
struct MSG message;


The only thing that this does is save you from typing something over and over, 
and gives you the option of giving names to structures that have more meaning.  
Sometimes it will be easier to write and for others to follow if you only type in 
MSG, instead of the 'struct' every time.

Say you have a finance program that defines a structure which holds checking 
information.  You keep the check information in a struct.  A way that makes 
sence to associate these two in your mind(and help anyone who might be looking 
at your program later figure out how it works) is to typedef this 'struct check' to 
just 'check':

struct check account[MAX];

void FillCheck(struct check *it);

--to--

check account[MAX];

void FillCheck(check *it);


When considered that there could be dozens of functions that take a check as a 
parameter, it greatly shortens the amount of code that needs to be written and 
gets rid of repetitive words.

That's all 'typedef' does; it changes the name of a data type into something that is 
shorter to type in, and thus makes the code a bit tider from all the repetitive words 
in function arguments.  It also makes a commonly used thing more program 
specific.

>>From this:

struct check account;

to:

check account;


The second one makes a bit more sence.  That struct isn't in the way of what this 
is saying: that the account is a check.  The struct word can be a bit of a 
hinderance to understanding in what each data is suppose to do.

I hope this makes sence :-).  It's hard for me to explain myself without repeating 
myself more than twice.
________________________________________________________
Signed: Fox
        sfox@xxxxxxxx.xxx

Unbeknowst to most computer users, the "Hertz" is the unit
of measurement for how much faster and smarter the computer
is compared to you.
To signoff send a mail to listserver@xxxx.xx.xx with 
  "signoff tcg" in the body of your message.