typedef a struct [C Coding styles]

M

MJ_India

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?
 
I

Ian Collins

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?

Which ever your shop uses. The question as like asking which is the
best flavour of ice cream.
 
N

Nate Eldredge

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?

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.
 
C

CBFalconer

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?

Well, not 1, which doesn't work in a C compiler.
 
N

Nick Keighley

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
 
M

MJ_India

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top