473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 18416
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: j6vflbl6vy6g8o001 | last post by:
Sorry if this is really dumb, but I've searched for an example like this and although can find many structures containing references back to themselves, none with the additional complexities I see...
2
by: Steven T. Hatton | last post by:
typedef struct { unsigned char e_ident; Elf32_Half e_type; Elf32_Half e_machine; Elf32_Word e_version; Elf32_Addr e_entry; Elf32_Off e_phoff; Elf32_Off e_shoff; Elf32_Word e_flags;...
4
by: Martin Vorbrodt | last post by:
why would you bother writing: typedef struct S { } S_t; instead of just: struct S { };
2
by: Neil McPhail | last post by:
I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete C Reference. I appreciate this may not have been the best idea, so no need to point this out! On page 546 there is a...
8
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;
2
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...
23
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
8
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.