473,791 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Typedef or not

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?
Thanks,

Ronny Mandal

--

Support bacteria - it's the only culture some people have!
Feb 28 '06 #1
8 2360


Ronny Mandal wrote:
Consider these two:

Typedef struct { int foo, int bar } FooBar; ^^^ - added
You can use the typedef name when declaring this structure.
Its a matter of prog. convenience.

FooBar *fb; // declares a variable 'fb' as a pointer to structure FooBar
FooBar fb1 = {1, 1}; // inits the members foo and bar with value '1'

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?
No FooBar *pfb would do !


Thanks,

Ronny Mandal

--

Support bacteria - it's the only culture some people have!


Feb 28 '06 #2
>Typedef struct { int foo, bar } FooBar;

struct FooBar { int foo, bar };


The only benefit out of this is that you dont have to use struct
keyword.

Cheers
Vishal

Feb 28 '06 #3
On Tue, 28 Feb 2006 17:59:39 +0530, Ravi Uday wrote:


Ronny Mandal wrote:
Consider these two:

Typedef struct { int foo, int bar } FooBar;

^^^ - added


You corrected a non-error ("int a, b" is fine although "int a, b;" is
preferred) and did not correct the error (Typedef != typedef).

--
Ben.
Feb 28 '06 #4
Ronny Mandal <ro*****@math.u io.no> wrote:
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?


Yes. There are, IYAM, only two cases in which this is useful:

- you are lazy and do not care about clarity;
- you are defining an abstract data type.

Richard
Feb 28 '06 #5
Ronny Mandal <ro*****@math.u io.no> writes:
Consider these two:

Typedef struct { int foo, bar } FooBar;

struct FooBar { int foo, bar };
It's important to post *correct* code. Both of the lines above have
syntax errors. It's "typedef", not "Typedef", and you need a semicolon
after the member declaration:

typedef struct { int foo, bar; } FooBar;

struct FooBar { int foo, bar; };

The meaning happened to be clear enough in this case, but errors like
this can often make it difficult or impossible to guess what you
really mean. If you're going to post code here, even a one-liner,
it's a good idea to run it through a compiler first, then
cut-and-paste it into your newsreader.
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?


For the cases shown, yes, the only real difference is that with the
typedef you refer to the type as "FooBar", and without the typedef you
refer to the type as "struct FooBar". The first line:
typedef struct { int foo, bar; } FooBar;
declares an *anonymous* structure type, then creates an alias "FooBar"
for that type. The second line:
struct FooBar { int foo, bar; };
declares a structure type called "struct FooBar".

If you want the structure to contain a pointer to itself (say, for a
linked list or binary tree), you need to have a name for the type
within the type declaration itself. The typedef doesn't give you
this; the alias doesn't exist until after the end of the struct
declaration. With a struct tag, it's easy:

struct FooBar {
int foo;
int bar;
struct FooBar *next;
};

One common approach to this is to use a struct tag *and* a typedef:

typedef struct FooBar {
int foo;
int bar;
struct FooBar *next;
} FooBar;

You now have to names for the same type, "struct FooBar" (which can be
used inside the struct declaration) and "FooBar" which can be used
only after the end of the declaration). You can use the same
identifier for both, or you can really confuse things by using two
different identifiers.

If you're creating an abstract type (i.e., if code that uses the type
shouldn't even know that it's a structure), it makes sense to use a
typedef. Otherwise, IMHO, it's much simpler just to use the struct
tag. It requires a few extra keystrokes, but the Great Keystroke
Shortage was resolved years ago.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 28 '06 #6
Ben Bacarisse <be********@bsb .me.uk> writes:
On Tue, 28 Feb 2006 17:59:39 +0530, Ravi Uday wrote:


Ronny Mandal wrote:
Consider these two:

Typedef struct { int foo, int bar } FooBar;

^^^ - added


You corrected a non-error ("int a, b" is fine although "int a, b;" is
preferred) and did not correct the error (Typedef != typedef).


Actually, the semicolon is required, even in C99.

Also, Ravi: please avoid using tab characters: the quoting prefixes
have now skewed your post.
Feb 28 '06 #7
# What woul be the only difference here; just that I can create new

Whatever makes it easier for you?

--
SM Ryan http://www.rawbw.com/~wyrmwif/
JUSTICE!
Justice is dead.
Mar 1 '06 #8
On Tue, 28 Feb 2006 20:41:34 GMT, Keith Thompson <ks***@mib.or g>
wrote:
Ronny Mandal <ro*****@math.u io.no> writes: [corrected]
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?


For the cases shown, yes, the only real difference is that with the
typedef you refer to the type as "FooBar", and without the typedef you
refer to the type as "struct FooBar". <snip>
If you want the structure to contain a pointer to itself (say, for a
linked list or binary tree), you need to [use the tag ...]

One common approach to this is to use a struct tag *and* a typedef:
[with the same name, or even different]


Agree so far; plus one other difference is that the struct-tag form
allows use of the same name in the same scope as an ordinary
identifier for something else, e.g. a variable. Whether it is a good
idea to do this is another kettle of fish entirely.

(Not in C++) You can even use it as a typedef for a _different_ type,
but if you do you deserve to be 'detained' in an unacknowledged CIA
prison for several decades. Well, maybe not quite that, but close. <G>

- David.Thompson1 at worldnet.att.ne t
Mar 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3640
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
14
4650
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further explanations, so I wonder is there any good using typedef ? and I also know that when a data type being typedefed become an abstract data type, so what exactly is an abstract data type, is it any good ? -- Posted via http://dbforums.com
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;
15
5689
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
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 {
12
15647
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
6
7349
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;
15
2574
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 ?
12
4655
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))();
16
2787
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the threads, there is sound advice ( to me at any rate) not to hide pointers behind typedefs. So, may I ask the group when the use of typedefs really makes sense? Sorry if this is somewhat general, but there are no exercises ( not that I am asking for...
0
9669
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
9515
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
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9029
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...
0
6776
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();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.