typedef struct

Discussion in 'C++' started by Martin Vorbrodt, Oct 31, 2005.

  1. why would you bother writing:

    typedef struct S {
    } S_t;

    instead of just:

    struct S {
    };

    and the just use S, or struct S (in plain C) ?

    what is the advantage of declaring struct S and then a typedef for it S_t
    ???
    Martin Vorbrodt, Oct 31, 2005
    #1
    1. Advertising

  2. Martin Vorbrodt

    Pete Becker Guest

    Martin Vorbrodt wrote:
    >
    > what is the advantage of declaring struct S and then a typedef for it S_t
    > ???
    >
    >


    In C you can create objects with just the typedef name:

    S_t object;

    That's shorter than having to write 'struct' everywhere:

    struct S object;

    Other than that, they're equivalent.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Oct 31, 2005
    #2
    1. Advertising

  3. "Martin Vorbrodt" <> wrote in message
    news:dk5qe8$fj9$...
    : why would you bother writing:
    :
    : typedef struct S {
    : } S_t;
    :
    : instead of just:
    :
    : struct S {
    : };
    :
    : and the just use S, or struct S (in plain C) ?
    :
    : what is the advantage of declaring struct S and then a typedef for it
    S_t
    : ???

    What I would typically see is more something like:
    typedef struct S_t { /*...*/ } S;

    This style trick is useful in C code, to allow one to use 'S'
    alone as a type identifier (instead of being required to always
    prepend the keyword 'struct' to refer to the struct type in C).

    The 'S_t' struct identifier is optional above. It can be useful
    when one wants to forward-declare 'S':
    typedef struct S_t S; // forward declaration of S, would not
    // be possible without 'S_t' in the decl.

    This typedef trick is totally pointless in C++, except when
    a more transparent backwards-compatibility with C is desired.


    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Oct 31, 2005
    #3
  4. Martin Vorbrodt wrote:
    > why would you bother writing:
    >
    > typedef struct S {
    > } S_t;
    >
    > instead of just:
    >
    > struct S {
    > };
    >
    > and the just use S, or struct S (in plain C) ?
    >
    > what is the advantage of declaring struct S and then a typedef for it S_t
    > ???


    The code that uses that struct under the typedef-id (S_t) would be the
    same in C++ or C (if you intend to make it source-portable).

    V
    Victor Bazarov, Oct 31, 2005
    #4
  5. "Victor Bazarov" <> wrote in message
    news:Jhu9f.44918$01.us.to.verio.net...
    : Martin Vorbrodt wrote:
    : > why would you bother writing:
    : >
    : > typedef struct S {
    : > } S_t;
    : >
    : > instead of just:
    : >
    : > struct S {
    : > };
    : >
    : > and the just use S, or struct S (in plain C) ?
    : >
    : > what is the advantage of declaring struct S and then a typedef for it
    S_t
    : > ???
    :
    : The code that uses that struct under the typedef-id (S_t) would be the
    : same in C++ or C (if you intend to make it source-portable).

    Note that in either case, portability can also be achieved
    by writing "struct S" for both C and C++. But this style
    feels very passé, if not confusing, to a C++ programmer.

    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Nov 1, 2005
    #5
    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. Replies:
    2
    Views:
    502
  2. DanielEKFA
    Replies:
    8
    Views:
    606
    DanielEKFA
    May 16, 2005
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  4. Steven T. Hatton
    Replies:
    2
    Views:
    678
  5. oor
    Replies:
    0
    Views:
    1,346
Loading...

Share This Page