469,352 Members | 1,648 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

typedef a struct [C Coding styles]

Style 1:
struct my_struct
{
...
};
typedef my_struct my_struct_t;

Style 2:
typedef struct my_struct
{
...
}my_struct_t;

Style 3:
typedef struct
{
...
}my_struct_t;

Which style should be preferred while coding and why?
Oct 9 '08 #1
7 17833
MJ_India wrote:
Style 1:
struct my_struct
{
...
};
typedef my_struct my_struct_t;

Style 2:
typedef struct my_struct
{
...
}my_struct_t;

Style 3:
typedef struct
{
...
}my_struct_t;

Which style should be preferred while coding and why?
Which ever your shop uses. The question as like asking which is the
best flavour of ice cream.

--
Ian Collins
Oct 9 '08 #2
MJ_India <ma************@gmail.comwrites:
Style 1:
struct my_struct
{
...
};
typedef my_struct my_struct_t;

Style 2:
typedef struct my_struct
{
...
}my_struct_t;

Style 3:
typedef struct
{
...
}my_struct_t;

Which style should be preferred while coding and why?
Style 1 is incorrect, I believe. I think you meant the last line to
read

typedef struct my_struct my_struct_t;

Style 3 has the disadvantage that the struct can't contain pointers to
its own type, e.g. for a linked list. These have to be declared as
'struct my_struct *', not as 'my_struct_t *'. On the other hand, it has
the advantage that it forces you to use 'my_struct_t' consistently, rather
than possibly using both 'my_struct_t' and 'struct my_struct' in
different parts of the code and causing confusion.

I personally prefer the corrected version of Style 1, because it is
easier to spot the definition of the type my_struct_t when it's on its
own line, rather than tacked onto the definition of struct my_struct as
in Style 2. It's also easier to find the definition by grepping for
'typedef.*my_struct_t'. But of course it is a matter of taste; there
are equally valid arguments the other way.

Actually, if practical, I would tend to avoid the typedef altogether,
and just use 'struct my_struct' throughout. I like to be reminded what
the type actually is.

Note: you phrased this question like a homework or exam question. If
you use my post as part of an answer to any sort of assignment or exam,
please remember to cite it as a source.
Oct 9 '08 #3
MJ_India wrote:
>
Style 1:
struct my_struct
{
...
};
typedef my_struct my_struct_t;

Style 2:
typedef struct my_struct
{
...
} my_struct_t;

Style 3:
typedef struct
{
...
} my_struct_t;

Which style should be preferred while coding and why?
Well, not 1, which doesn't work in a C compiler.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 10 '08 #4
On 10 Oct, 00:10, MJ_India <mail.mohitj...@gmail.comwrote:
3 styles of typedef'ing: typedef struct my_struct my_struct_t
Which style should be preferred while coding and why?
Use the style already in the project. If starting from scratch,
use the style predominantly in use by the people likely
to be working on the code. Otherwise, pick one you like.

Two suggestions: don't bother with the typedef, but if you do,
don't use a name ending in _t. The typedef is useful for opaque
types, but otherwise obscures the code. Names ending in
_t should be avoided because POSIX reserves such names.

--
William Pursell
Oct 10 '08 #5
On 10 Oct, 05:48, William Pursell <bill.purs...@gmail.comwrote:
On 10 Oct, 00:10, MJ_India <mail.mohitj...@gmail.comwrote:
3 styles of typedef'ing: typedef struct my_struct my_struct_t
Which style should be preferred while coding and why?

Use the style already in the project. *If starting from scratch,
use the style predominantly in use by the people likely
to be working on the code. *Otherwise, pick one you like.
yes
Two suggestions: don't bother with the typedef,
this is, of course, a style question. I dislike the "struct"s
that litter the code if you don't use the typedef (and the C++
people seem to agree with me).

but if you do, don't use a name ending in _t.
agreed

>*The typedef is useful for opaque
types, but otherwise obscures the code.
no having "structs" all over the place obscures the code

>*Names ending in
_t should be avoided because POSIX reserves such names.

--
Nick Keighley

Oct 10 '08 #6
MJ_India said:
Style 1:
struct my_struct
{
...
};
typedef my_struct my_struct_t;

Style 2:
typedef struct my_struct
{
...
}my_struct_t;

Style 3:
typedef struct
{
...
}my_struct_t;

Which style should be preferred while coding and why?
You should prefer Style 4:

struct my_struct_
{
...
};

typedef struct my_struct_ my_struct;

This style separates type definition (which is *not* what typedef does)
from typename synonym creation (which *is* what typedef does), and retains
a strong correspondence between type name and type synonym without the
disadvantage of using the same name for both (which can confuse some
debuggers, and in any case is a pain for grep).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 10 '08 #7
Yes I commited a mistake in style 1. It should have been typedef
_struct_ my_struct my_struct_t;

Thank you for all your expert and valuable replies.
Oct 10 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by j6vflbl6vy6g8o001 | last post: by
2 posts views Thread by Steven T. Hatton | last post: by
4 posts views Thread by Martin Vorbrodt | last post: by
2 posts views Thread by Neil McPhail | last post: by
2 posts views Thread by Immo Birnbaum | last post: by
23 posts views Thread by myth.drannon | last post: by
8 posts views Thread by Mohammad Omer Nasir | last post: by
8 posts views Thread by cman | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.