473,399 Members | 3,603 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,399 software developers and data experts.

typedefing a struct

Is there any functional difference (or any other reason to prefer
one over the other) between these two methods:

typedef struct mystruct {
int a;
int b;
} mystruct;

struct mystruct {
int a;
int b;
};
typedef struct mystruct mystruct;
Sep 13 '07 #1
15 2294
In article <rWjGi.168539$rX4.15093@pd7urf2no>,
David Marsh <dm****@mail.comwrote:
>Is there any functional difference (or any other reason to prefer
one over the other) between these two methods:

typedef struct mystruct {
int a;
int b;
} mystruct;

struct mystruct {
int a;
int b;
};
typedef struct mystruct mystruct;
No, but there is sometimes reason to do it the other way round:

typedef struct mystruct mystruct;

struct mystruct {
int a;
int b;
};

Because you can then use the type name "mystruct" in and before the
definition of the struct, which is useful if you have structures that
point to each other.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 14 '07 #2
David Marsh <dm****@mail.comwrites:
Is there any functional difference (or any other reason to prefer one
over the other) between these two methods:

typedef struct mystruct {
int a;
int b;
} mystruct;

struct mystruct {
int a;
int b;
};
typedef struct mystruct mystruct;
I don't believe there's any real difference. Note that in both cases,
any reference to the type within its own definition (say, if a
mystruct contains a pointer to a mystruct) has to use the name 'struct
mystruct', since the typedef name doesn't exist yet.

You could even drop the tag, and just declare

typedef struct {
int a;
int b;
} mystruct;

since you never use the struct tag name anyway. But you'll need the
tag if you ever add a mystruct pointer as a member.

Using the same name for the struct tag and for the typedef is
perfectly legal, but may cause problems in some IDEs. If this induces
you to use different names, pick a consistent convention to avoid
confusion.

Richard Heathfield has argued for the second form on stylistic
grounds, on the basis that since two names are being declared ('struct
mystruct' and 'mystruct'), there should be two declarations. I'll let
him refute my blatant misrepresentation of what he actually said. 8-)}

Others, myself included, have argued that in most cases typedefs for
strutures are superfluous. Your type already has a perfectly good
name, 'struct mystruct'; it doesn't need another. Using the 'struct'
keyword every time you refer to the type reminds the reader of the
relevant fact that the type is a structure type. If this fact is not
relevant, i.e., if you're creating an opaque type like FILE, then
using a typedef makes sense.

Plenty of very smart people disagree with me on this point, and
believe instead that having a one-word name for the type is
worthwhile.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '07 #3
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <rWjGi.168539$rX4.15093@pd7urf2no>,
David Marsh <dm****@mail.comwrote:
>>Is there any functional difference (or any other reason to prefer
one over the other) between these two methods:

typedef struct mystruct {
int a;
int b;
} mystruct;

struct mystruct {
int a;
int b;
};
typedef struct mystruct mystruct;

No, but there is sometimes reason to do it the other way round:

typedef struct mystruct mystruct;

struct mystruct {
int a;
int b;
};

Because you can then use the type name "mystruct" in and before the
definition of the struct, which is useful if you have structures that
point to each other.
Note that this is vulnerable to typos. For example, this:

typedef struct my_struct mystruct;

struct mystruct {
int a;
int b;
mystruct *next;
};

compiles without error (but it will fail as soon as you try to declare
an object of type 'mystruct'). The problem is the added underscore in
the struct tag. 'struct my_struct' is an incomplete type, which is
legal in that context, even though you *meant* to use 'struct
mystruct'.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '07 #4
"David Marsh" <dm****@mail.comwrote in message
news:rWjGi.168539$rX4.15093@pd7urf2no...
Is there any functional difference (or any other reason to prefer one over
the other) between these two methods:
[...]

FWIW, here is how I personally do it:
typedef struct my_struct_s my_struct_t;

struct my_struct_s {
int a;
int b;
my_struct_t *c;
};
I postfix an '_s' for the struct name and a '_t' for the typedef name. It
seems to help me differentiate between the two quite easily.

Sep 14 '07 #5
Keith Thompson said:

<snip>
Using the same name for the struct tag and for the typedef is
perfectly legal, but may cause problems in some IDEs. If this induces
you to use different names, pick a consistent convention to avoid
confusion.
My preference: struct mystruct_, and typedef struct mystruct_ mystruct.
Richard Heathfield has argued for the second form on stylistic
grounds, on the basis that since two names are being declared ('struct
mystruct' and 'mystruct'), there should be two declarations. I'll let
him refute my blatant misrepresentation of what he actually said.
Ta Keith. Yeah, I think "should" is a bit strong. I have my own
preference, but C is flexible to cope with more than one preference!

I remember being confused by td s { foo } s; - by which I mean I thought
I knew what it meant, and was wrong, and did not discover this for some
years. On reflection, much of my coding style is based on the idea of
avoiding constructs that confused me in the past. This is probably why
I prefer sizeof x to sizeof(x), for example.
Others, myself included, have argued that in most cases typedefs for
strutures are superfluous. Your type already has a perfectly good
name, 'struct mystruct';
FCOV "perfectly good" :-)

<snip>
Plenty of very smart people disagree with me on this point, and
believe instead that having a one-word name for the type is
worthwhile.
It must, however, also be pointed out that plenty of very smart people
*agree* with you. (I am not one of them.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 14 '07 #6
Chris Thomasson said:

<snip>
I postfix an '_s' for the struct name and a '_t' for the typedef name.
It seems to help me differentiate between the two quite easily.
....and violates a POSIX rule, so keep your nose out of c.u.p. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 14 '07 #7
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:x9******************************@bt.com...
Chris Thomasson said:

<snip>
>I postfix an '_s' for the struct name and a '_t' for the typedef name.
It seems to help me differentiate between the two quite easily.

...and violates a POSIX rule, so keep your nose out of c.u.p. :-)
[...]

DAMN! I totally forgot about how the POSIX namespace reserves the '_t'
postfix; thank you for reminding me.

Sep 14 '07 #8
On Thu, 13 Sep 2007 23:43:19 GMT, David Marsh <dm****@mail.comwrote:
>Is there any functional difference (or any other reason to prefer
one over the other) between these two methods:

typedef struct mystruct {
int a;
int b;
} mystruct;

struct mystruct {
int a;
int b;
};
typedef struct mystruct mystruct;
Worth reading:

http://web.torek.net/torek/c/types2.html

--
jay
Sep 14 '07 #9
On Thu, 13 Sep 2007 18:47:11 -0700, Chris Thomasson wrote:
I postfix an '_s' for the struct name and a '_t' for the typedef name. It
seems to help me differentiate between the two quite easily.
What's the need for that? The former is always preceded by the
keyword struct and the latter is never.

(typedef struct foo foo; is one of the things I dislike without
being able to find any rational reasons whatever for doing so. But
I do agree that type names should be recognizable at a glance. I
still haven't found an alternative to the _t suffix which POSIX
reserves to implementations.)
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 14 '07 #10
Army1987 said:
On Thu, 13 Sep 2007 18:47:11 -0700, Chris Thomasson wrote:
>I postfix an '_s' for the struct name and a '_t' for the typedef
name. It seems to help me differentiate between the two quite easily.
What's the need for that? The former is always preceded by the
keyword struct and the latter is never.

(typedef struct foo foo; is one of the things I dislike without
being able to find any rational reasons whatever for doing so.
Here's a rational reason for you (or, if you prefer, a
rationalisation!): it confuses Visual Studio's Intellisense "jump to
def" functionality, albeit not disablingly so.
But
I do agree that type names should be recognizable at a glance. I
still haven't found an alternative to the _t suffix which POSIX
reserves to implementations.)
I use typedef struct foo_ foo, on the grounds that I'll hardly ever see
the struct tag, but I'll see the synonym a lot.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 14 '07 #11
Richard Heathfield <rj*@see.sig.invalidwrites:
Army1987 said:
>On Thu, 13 Sep 2007 18:47:11 -0700, Chris Thomasson wrote:
>>I postfix an '_s' for the struct name and a '_t' for the typedef
name. It seems to help me differentiate between the two quite easily.
What's the need for that? The former is always preceded by the
keyword struct and the latter is never.

(typedef struct foo foo; is one of the things I dislike without
being able to find any rational reasons whatever for doing so.

Here's a rational reason for you (or, if you prefer, a
rationalisation!): it confuses Visual Studio's Intellisense "jump to
def" functionality, albeit not disablingly so.
And, if it confuses that (and tags and cscope) then it will confuse
people. It confuses me. I dont want to "think" when looking at a
declaration. It should be obvious. I like to see "struct" there in the
variable declaration. I then know it's a struct ....
>
>But
I do agree that type names should be recognizable at a glance. I
still haven't found an alternative to the _t suffix which POSIX
reserves to implementations.)

I use typedef struct foo_ foo, on the grounds that I'll hardly ever see
the struct tag, but I'll see the synonym a lot.
Sep 14 '07 #12
"Army1987" <ar******@NOSPAM.ita crit dans le message de news:
pa****************************@NOSPAM.it...
On Thu, 13 Sep 2007 18:47:11 -0700, Chris Thomasson wrote:
>I postfix an '_s' for the struct name and a '_t' for the typedef name. It
seems to help me differentiate between the two quite easily.
What's the need for that? The former is always preceded by the
keyword struct and the latter is never.

(typedef struct foo foo; is one of the things I dislike without
being able to find any rational reasons whatever for doing so. But
I do agree that type names should be recognizable at a glance. I
still haven't found an alternative to the _t suffix which POSIX
reserves to implementations.)
I don't see the point of inventing 2 different identifiers for the same
purpose. Indeed the definition `typedef struct foo foo;' is implicit in C++
for all struct foo declared or defined.
The consequence of the typedef is that you can no longer use `foo' to name
struct foo instances or any variables. But that is not really an
inconvenient as it would be quite confusing anyway.

I guess the real question is what convention to follow for naming structures
and other types.
The _t suffix is in wide use and recognized by editors, but it violates
POSIX constraints.
Using an initial capital is also in wide use in OOP but give C code a bad
smell of java.
Using all caps would be consistent with FILE and Microsoft C based APIs but
looks ugly.
I cannot settle for one over the others...

--
Chqrlie.
Sep 14 '07 #13
Chris Thomasson wrote:
"David Marsh" <dm****@mail.comwrote in message
news:rWjGi.168539$rX4.15093@pd7urf2no...
>Is there any functional difference (or any other reason to prefer one
over the other) between these two methods:
[...]

FWIW, here is how I personally do it:
typedef struct my_struct_s my_struct_t;

struct my_struct_s {
int a;
int b;
my_struct_t *c;
};
I postfix an '_s' for the struct name and a '_t' for the typedef name.
It seems to help me differentiate between the two quite easily.
To typedef, or not to typedef, that's a question I have never agreed
with myself on. For low-level work, I prefer little obfuscation, and
aliasing by typedef's, but for high-level API's, those typedef's tend to
sneak in.
My naming convention is similar to yours, but with an important difference:

typedef struct my_struct MY_STRUCT_T;

struct my_struct
{
int a;
int b;
MY_STRUCT_T *c;
};

the "_T" suffix and uppercase, rarely pollute the name space.

--
Tor <torust [at] online [dot] no>
Sep 14 '07 #14
"Charlie Gordon" <ne**@chqrlie.orgwrites:
[...]
I guess the real question is what convention to follow for naming
structures and other types. The _t suffix is in wide use and
recognized by editors, but it violates POSIX constraints. Using an
initial capital is also in wide use in OOP but give C code a bad smell
of java. Using all caps would be consistent with FILE and Microsoft C
based APIs but looks ugly. I cannot settle for one over the others...
A digression:

I suspect the reason FILE is in all-caps is that it was invented
before typedefs were introduced to the language, and thus must have
originally been a macro. When typedefs were invented, it was too late
to change it.

The definition might have been something like:

struct file { /* ... */ };
#define FILE struct file

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '07 #15
Army1987 wrote:
I still haven't found an alternative to the _t suffix
which POSIX reserves to implementations.
I use _type.

http://www.mindspring.com/~pfilandr/...ver/e_driver.h

#define STRUCTURES 0 /* 0 or 1, This is the line to change */

#if STRUCTURES == 0 /* Not this one */

#define E_TYPE long unsigned
#define D_TYPE E_TYPE
#define GT(A, B) (*(A) *(B))

#else

#define E_TYPE struct {/**/char array[20];/**/d_type data;}
#define D_TYPE long unsigned /* unsigned, double */
#define GT(A, B) ((A) -data (B) -data)

#endif

typedef D_TYPE d_type;
typedef E_TYPE e_type;

--
pete
Sep 14 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: Fred | last post by:
Can anyone explain what the point of this is? For example, code like: typedef struct { int alloc; char *data; } my_struct; typedef my_struct my_type_t;
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.