0
typedef struct hash_table_data
{
  int key;
  int data;
  struct hash_table_data* next;
  struct hash_table_data* prev;
}hash_table_data;

typedef struct hash_table
{
  int num_entries;
  struct hash_table **entries;
}hash_table;

VERSUS

struct hash_table_data_
{
  int key,data;
  struct hash_table_data_ *next,*prev;
};
typedef struct hash_table_data_ hash_table_data;

struct hash_table_
{
  int num_entries;
  struct hash_table_data_ **entries;
};
typedef struct hash_table_ hash_table;
nick_name
  • 1,353
  • 3
  • 24
  • 39
  • Isn't the first just a lot better style? – nick_name Jul 25 '11 at 21:50
  • In my opinion (and it's up for debate) the latter example is better style; because, it doesn't commingle the struct definition with the typedef. However, there are lots of people who feel just as strongly otherwise. It is best to throw away such opinions when modifying or extending other's code. Do what they do. When writing your own new code from scratch, that's really the only time is is appropriate to exercise your opinion on such a topic. – Edwin Buck Jul 25 '11 at 22:00
  • I wonder why certain rather rare topics seem to show up in bunches on SO periodically? I can't remember the last time I've read a question having to do with obscure details of struct tag names, and here we have two in the last 24 hours (http://stackoverflow.com/questions/6812065/c-style-struct-declaration). – Michael Burr Jul 25 '11 at 22:15
  • I imagine it's a university which has good word of mouth that SO can help / solve questions related to a curriculum. That coupled with class getting out and a professor not being clear on a point. A wild guess to be sure, but it would explain a tight correlation of similar problems. – Edwin Buck Jul 26 '11 at 19:28

5 Answers5

2

In the second example, you have an extra names in action: struct hash_table_data_ and struct hash_table_

In the first example, you effectively hide the name struct hash_table_data with a typedef which renames struct hash_table_data to the shorter hash_table_data.

Think of it like so:

typedef (something that happens to be struct X) (to) X

vs

define a struct X
typedef struct X (to) X

Now in the latter example, you actually don't do exactly what the former example does. In the latter example, you do

define a struct X_
typedef struct X_ (to) X

The key here is that you have an "extra" struct X_ which could be used directly.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • He has both names in both. He'd have to change the first example to `typedef struct { ...` from `typedef struct hash_table_data { ...` to not have the name `struct hash_table_data` available { – Paul Jul 25 '11 at 21:48
  • Do they achieve exactly the same end? If I were to use one versus the other, would there be a difference? – nick_name Jul 25 '11 at 21:50
  • Ah, so I have some redundancy... I could delete the first typedef struct hash_table_data or the hash_table_data following the closing bracket...? – nick_name Jul 25 '11 at 21:52
  • In the sense that they both define some name X to expand to "struct X" or "struct X_" which will then refer to the same structure layout, yes. However, one will expand to "struct X", while the other will expand to "struct X_". In the first example, you can't type "struct X" because the "X" will expand out to "struct X" leaving you with "struct struct X", which is nonsense. In the second example, you could either use "X" which will expand out to "struct X_" or you could use "struct X_" directly. It is in the ability to use "struct X_" directly that differentiates the two. – Edwin Buck Jul 25 '11 at 21:54
  • Note that the actual field expansion will be the same whether it is a "struct X" or a "struct X_" because both structures actually list the same fields in the same order. So really it is only a difference in the name, not the actual layout of the fields in memory. – Edwin Buck Jul 25 '11 at 21:55
  • If you wanted to delete the typedef in the first example, the typedef at the beginning of the line should be removed, and the "name" at the end of the line (which is at the end of the struct) should be removed. That would leave "struct X {...};" defining your struct. If you wanted to add it back in on it's own line it would then later be declared "typedef struct X X;" Typedefs take a little work in getting used to reading them, everything between the typedef and the last name is what the last name will expand out to. – Edwin Buck Jul 25 '11 at 21:58
2

Effectively none whatsoever. (Removed link since some people can't read in context.)

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
1

The only difference is that in the first example the structure's tag names are the same as the typedef name, and in the second example they're slightly different (they have a trailing underscore).

There's no need for the typedef name to be different (or the same), since struct tag names are in a different namespace. See C style struct declaration for some additional detail.

As a matter of style, I prefer my member declarations to be one to a line (as in the first example), but that's strictly a style preference; there's no consequence to the structure.

Community
  • 1
  • 1
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

They're just two different ways to end up with the same result.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

The first one is shorter. Other than that they are the same.

Paul
  • 139,544
  • 27
  • 275
  • 264