473,700 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18443
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.comwrite s:
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_st ruct_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...@g mail.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
1469
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 here. I would really appreciate an explanation of what this is / is doing! Thanks! typedef struct SocketError { int socketErrno; unsigned short socketPort;
2
2238
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; Elf32_Half e_ehsize;
4
13248
by: Martin Vorbrodt | last post by:
why would you bother writing: typedef struct S { } S_t; instead of just: struct S { };
2
2029
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 mailing list program with a struct defined thus: struct address { char name; char street; char city;
8
2374
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
3055
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 aspects of pointers. I'd appreciate if anybody could help me with the following problem: I tried to learn how to use malloc, free, and the * and & operators. I started with a few simple lines of code like:
23
5806
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
8
28460
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
40465
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 familiar with "typedef int NUMBER", but how does it work with structures. Tilak
0
8728
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
9217
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9076
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...
1
8974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8926
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5903
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
4659
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2030
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.