473,782 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ 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 13257
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*******@pocz ta.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.********@com Acast.net> wrote in message
news:Jh******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
: 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
3055
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 { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
8
2382
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
5684
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 everything in a /*...*/ comment and appending the 'hail world' code. Is there an easier, and, let's say, more
2
3059
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 aspects of pointers. I'd appreciate if anybody could help me with the following problem: I tried to learn how to use malloc, free, and the * and & operators. I started with a few simple lines of code like:
16
3846
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
7347
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 (condition) typedef char T; #else typedef short T;
8
2359
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', and the second demands that I prefix with struct? struct FooBar fb, *pfb?
15
2573
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 ! ) what is it supposed to do ?
17
3317
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
40471
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 familiar with "typedef int NUMBER", but how does it work with structures. Tilak
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.