This is a discussion on Difference b/w struct and typedef struct within the C and C++ forums, part of the Programming Talk category; whats' the diff b/w defining a structure in C like 1) struct <structtag> { } ( and ) 2) typedef struct { }&...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
whats' the diff b/w defining a structure in C like
1) struct <structtag> { } ( and ) 2) typedef struct { }<structtag> In the first case you are naming the structure and creating variable of that type later using that name. In the second case you are using typedef and before you close the structure you have to name the structure. For eg: struct node { int data; }; struct node d1; // d1 is a structure variable typedef struct { int data; }d1; I hope i have cleared your doubt. HVG |
|
|||
|
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; }; ------------------------------------------------------------ |
|
|||
|
Hello friend,
Actually typedef struct is used for rename of the structure like as struct employee { int a; char[20] ename; }; in this we can write it as typedef struct employee emp so now onwards we can use employee like as emp . by this we can reduce our tag name length. |
|
|||
|
hi laxman_balu,
thanks 4 ur' clarification.. got cleared.. Best Wishes.. Quote:
|
|
|||
|
Hi, This post is very informative, however I would like some specific information. If someone can help me then please send me a private message. Best Regards,
|
|
|||
|
struct is used to group the variables ie., you have to create new data type
struct employee { int a; char[20] ename; }; typedef is used to create a readable name(ex: alias to the datatype) to the existing data type; |
![]() |
| Thread Tools | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 06-15-2004 07:00 AM |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 06-01-2004 07:01 AM |
| comp.lang.c FAQ list Table of Contents | Steve Summit | Tech FAQ | 0 | 06-01-2004 07:00 AM |
| comp.lang.c Answers to Frequently Asked Questions (FAQ List) | Steve Summit | Tech FAQ | 0 | 06-01-2004 07:00 AM |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 05-15-2004 07:00 AM |