Difference b/w struct and typedef struct

Post date: Apr 29, 2011 8:58:22 AM

1) struct <structtag>

{

}

2) typedef struct

{

}<structtag>

if you want to define a structure you have to follow (1)

later if you want to use structure created by method (1) you must use

following notation in order to declare a variable e.g.

struct <structtag> var_name; e.g.

struct Person{

char name[30];

int height;

};

if you want to declare variable of Person structure you have to follow as..

struct Person Baira, John;

Here keyword <struct> is complusory with <Person> any were you want to use that structure.

whereas typedef defines a type what it proceeds like...

typedef float CURRENCY

here CURRENCY is now a type and you can declare variables of type CURRENCY like

CURRENCY usd, euro;

following the above rule, typedef will defince a type of <structtag>

e.g.

typedef struct {

char name[30];

int height;

} Person;

now Person is a type; and you can declare variables of Person type e.g.

Person Baira, John;

above structure can also be written as

typedef struct Person{

char name[30];

int height;

};