473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

typedef struct

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
4 13221
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
8
by: J Krugman | last post by:
My compiler complains if I do something like this typedef struct { node *next; } node; but it's OK with typedef struct superfluous_identifier { superfluous_identifier *next;
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
8
by: Ronny Mandal | last post by:
Consider these two: Typedef struct { int foo, bar } FooBar; struct FooBar { int foo, bar }; What woul be the only difference here; just that I can create new instances by 'Foobar fb, *pfb',...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
17
by: Steve Carter | last post by:
Can someone please explain why the following is not possible? typedef foobar { int x; foobar *next; /* why can't we do this? */ }; Thanks
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.