Exforsys
+ Reply to Thread
Results 1 to 7 of 7

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 ...

  1. #1
    Bairaviuthra is offline Junior Member Array
    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>


  2. #2
    hvg_99 is offline Junior Member Array
    Join Date
    May 2007
    Answers
    9
    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


  3. #3
    Mukhtar Ahmad is offline Junior Member Array
    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;
    };

    ------------------------------------------------------------


  4. #4
    laxman_balu is offline Junior Member Array
    Join Date
    May 2007
    Location
    hyderabad
    Answers
    1

    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.


  5. #5
    Bairaviuthra is offline Junior Member Array
    Join Date
    Jul 2007
    Answers
    3

    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.



  6. #6
    Nabeel is offline Junior Member Array
    Join Date
    Jul 2007
    Answers
    1
    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,


  7. #7
    Mideen is offline Software Programmer Array
    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



Latest Article

Network Security Risk Assessment and Measurement

Read More...