Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Typedef or not

0 views
Skip to first unread message

Ronny Mandal

unread,
Feb 28, 2006, 7:14:41 AM2/28/06
to
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!

Ravi Uday

unread,
Feb 28, 2006, 7:29:39 AM2/28/06
to

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 !

manoch...@gmail.com

unread,
Feb 28, 2006, 7:29:38 AM2/28/06
to
>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

Ben Bacarisse

unread,
Feb 28, 2006, 9:01:35 AM2/28/06
to
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.

Richard Bos

unread,
Feb 28, 2006, 9:24:41 AM2/28/06
to
Ronny Mandal <ron...@math.uio.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

Keith Thompson

unread,
Feb 28, 2006, 3:41:34 PM2/28/06
to
Ronny Mandal <ron...@math.uio.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_Keith) 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.

Micah Cowan

unread,
Feb 28, 2006, 4:10:18 PM2/28/06
to
Ben Bacarisse <ben.u...@bsb.me.uk> writes:

Actually, the semicolon is required, even in C99.

Also, Ravi: please avoid using tab characters: the quoting prefixes
have now skewed your post.

SM Ryan

unread,
Mar 1, 2006, 1:58:36 AM3/1/06
to
# 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.

Dave Thompson

unread,
Mar 5, 2006, 9:21:14 PM3/5/06
to
On Tue, 28 Feb 2006 20:41:34 GMT, Keith Thompson <ks...@mib.org>
wrote:

> Ronny Mandal <ron...@math.uio.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.net

0 new messages