Exforsys

Online Training

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 struct { }&...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-02-2007, 06:14 AM
Junior Member
 
Join Date: Jul 2007
Posts: 3
Bairaviuthra is on a distinguished road
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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-03-2007, 06:38 AM
Junior Member
 
Join Date: May 2007
Posts: 9
hvg_99 is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-03-2007, 07:56 AM
Junior Member
 
Join Date: May 2007
Posts: 12
Mukhtar Ahmad is on a distinguished road
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;
};

------------------------------------------------------------
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-04-2007, 03:28 AM
Junior Member
 
Join Date: May 2007
Location: hyderabad
Posts: 1
laxman_balu is on a distinguished road
Post 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-05-2007, 03:50 AM
Junior Member
 
Join Date: Jul 2007
Posts: 3
Bairaviuthra is on a distinguished road
Thumbs up Thank u all..

hi laxman_balu,
thanks 4 ur' clarification.. got cleared.. Best Wishes..

Quote:
Originally Posted by laxman_balu View Post
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-31-2007, 04:51 AM
Junior Member
 
Join Date: Jul 2007
Posts: 1
Nabeel is on a distinguished road
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,
__________________
Pakistan Property | Wholesale Suppliers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-16-2008, 11:06 AM
Software Programmer
 
Join Date: Oct 2008
Location: India
Posts: 3
Mideen is on a distinguished road
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;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

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


All times are GMT -4. The time now is 08:52 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.