
- Forum
- Programming Talk
- C and C++
- Difference b/w struct and typedef struct
Difference b/w struct and typedef struct
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 ...
-
07-02-2007, 05:14 AM #1
- Join Date
- Jul 2007
- Answers
- 3
Difference b/w struct and typedef struct
whats' the diff b/w defining a structure in C like
1) struct <structtag>
{
}
( and )
2) typedef struct
{
}<structtag>
-
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
-
07-03-2007, 06:56 AM #3
- Join Date
- May 2007
- Answers
- 12
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;
};
------------------------------------------------------------
-
07-04-2007, 02:28 AM #4
- Join Date
- May 2007
- Location
- hyderabad
- Answers
- 1
clarification of u r doubt
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.
-
07-05-2007, 02:50 AM #5
- Join Date
- Jul 2007
- Answers
- 3
Thank u all..
hi laxman_balu,
thanks 4 ur' clarification.. got cleared.. Best Wishes..
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, 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,
-
10-16-2008, 10:06 AM #7
- Join Date
- Oct 2008
- Location
- India
- Answers
- 3
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;
-
Sponsored Ads

Reply With Quote






