7

Ignoring (with difficulty) Occam's Razor which would seem to put this quickly to rest, what advantage would this have:

typedef struct s_header {
    struct s_header *next;
    //...
} Header;

over this:

typedef struct header {
    struct header *next;
    //...
} header;

?

This question is based on an argument I was having in the comments to this codereview answer. My point there was diluted by also pointing out an error (which I believe would be easier to avoid by using the same name everywhere).

  • A better question would be why you are using typedef in the first place. You typedef something when 1. it's data is inaccessible or 2. the data is not supposed to be edited by hand. In both cases you will probably only use the type in one header, in which case you can omit the struct name. Do remember that every name you add to the global namespace will increase the posibility of a collision. That said, I personally use PascalCase to typedef pointers, posix_t for special, common types (like string_t), and used to use overlapping names before namespace cluttering became problematic. – YoYoYonnY Mar 3 '16 at 0:15
  • I use typedefs to make a shorter name for declaring variables or to declare the semantic meaning of a set of variables. Your list of acceptable uses seems very restrictive. – luser droog Mar 3 '16 at 0:50
  • It really depends on the project you're working on. With a small application on a embedded system you can probably get away with alot more than with a full-blown Kernel: kernel.org/doc/Documentation/CodingStyle – YoYoYonnY Mar 3 '16 at 14:16
7

Compatibility with ancient compilers, where using the same name caused a name collision:

Why is the structure name different from typedef name?

This is a holdover from very early versions of the C language where structure tags, union tags, and typedefs were kept in the same namespace. Consequently, you couldn’t say typedef struct XYZ { ... } XYZ;. At the open brace, the compiler registers XYZ as a structure tag name, and then when XYZ appears a second time, you get a redeclaration error. The standard workaround for this was to make the structure tag name a minor modification of the typedef name, most typically by putting the word tag in front.

The C language standardization process separated the structure and typename name spaces, so this workaround is no longer necessary,

(from Why are structure names different from their typedef names? - The Old New Thing)

  • 1
    That's even more ancient than K&R-C, right? It certainly was never standard C, which begins with C89... – Deduplicator Feb 17 '16 at 19:47
  • @Deduplicator Windows 1.0 was released in 1985. (Not sure if it already used the same windows API we use nowadays) – CodesInChaos Feb 18 '16 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.