By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

typedef struct

P: n/a
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
???
Oct 31 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
Oct 31 '05 #2

P: n/a
"Martin Vorbrodt" <mv*******@poczta.onet.pl> wrote in message
news:dk**********@news.onet.pl...
: 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
Oct 31 '05 #3

P: n/a
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
Oct 31 '05 #4

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Jh*******************@newsread1.mlpsca01.us.t o.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
Nov 1 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.