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

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

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

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. Jordi Vilar
    Replies:
    5
    Views:
    2,522
    Jonathan Turkanis
    Feb 18, 2004
  2. Jordi Vilar
    Replies:
    5
    Views:
    19,021
    Jonathan Turkanis
    Feb 18, 2004
  3. Patricia  Van Hise

    structs with fields that are structs

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

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    943
    Chris Hauxwell
    Apr 27, 2004
  5. Paminu
    Replies:
    5
    Views:
    881
    Eric Sosman
    Oct 11, 2005
  6. Daniel Rudy
    Replies:
    15
    Views:
    1,720
    Keith Thompson
    Apr 10, 2006
  7. Steve Carter

    typedef, structs, and pointers

    Steve Carter, Jul 18, 2006, in forum: C Programming
    Replies:
    17
    Views:
    654
    Simon Biber
    Jul 20, 2006
  8. Tuan  Bui
    Replies:
    14
    Views:
    808
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...