Typedef and Structs

Discussion in 'C++' started by kasiyil, Sep 1, 2006.

  1. kasiyil

    kasiyil Guest

    Hello everyone,

    I have a question about using typedef with structure definitions. Here
    is the example;

    typedef struct MyStruct {
    ...
    ..
    ..
    } MyStruct_t;

    What is the effect of using typedef in this example? What is the
    difference of it between the example below

    struct MyStruct_t {
    ...
    ..
    ..
    };

    Thanks for your answers...
     
    kasiyil, Sep 1, 2006
    #1
    1. Advertising

  2. kasiyil wrote:
    > I have a question about using typedef with structure definitions.
    > Here is the example;
    >
    > typedef struct MyStruct {
    > ...
    > ..
    > ..
    > } MyStruct_t;
    >
    > What is the effect of using typedef in this example? What is the
    > difference of it between the example below
    >
    > struct MyStruct_t {
    > ...
    > ..
    > ..
    > };


    The effect is that in the former case you have two names, 'MyStruct',
    a type-id, and 'MyStruct_t', a typedef-id, which is a synonym for it;
    and in the latter case you have only one name, 'MyStruct_t' which is
    a type-id. The difference is minimal, as you can see.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 1, 2006
    #2
    1. Advertising

  3. kasiyil

    Bart Guest

    kasiyil wrote:
    <snip>
    > typedef struct MyStruct {
    > ...
    > ..
    > ..
    > } MyStruct_t;
    >
    > What is the effect of using typedef in this example? What is the
    > difference of it between the example below
    >
    > struct MyStruct_t {
    > ...
    > ..
    > ..
    > };


    In C it is necessary to use a typedef if you want to refer to a struct
    type without using the keyword struct. In C++ this is no longer
    necessary, but there is lots of legacy code that still does this.

    Regards,
    Bart.
     
    Bart, Sep 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    651
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    565
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    651
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,424
    Keith Thompson
    Apr 10, 2006
  5. Steve Carter

    typedef, structs, and pointers

    Steve Carter, Jul 18, 2006, in forum: C Programming
    Replies:
    17
    Views:
    517
    Simon Biber
    Jul 20, 2006
Loading...

Share This Page