0

Regarding structures, are there any reasons not to use typedef?

like:

typedef struct{
    int member1;
    unsigned long member2;
    char *member3;
} myType;

instead of:

struct myType{
    int member1;
    unsigned long member2;
    char *member3;
};
phoxis
  • 60,131
  • 14
  • 81
  • 117
2013Asker
  • 2,008
  • 3
  • 25
  • 36

2 Answers2

1

Typedefs can make debugging difficult, since instances of myType will appear as anonymous type unless the debugger is smart enough to join the dots. For safety's sake, I prefer doing both:

typedef struct tag_myType {
    ⋮
} myType;

If I had to forgo one or the other, however, I'd live with writing struct myType everywhere, for the same reason.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Note that the `_t` is technically makes it a reserved name. It's also unnecessary; `typedef struct myType { ... } myType;` works just fine. – Oliver Charlesworth Aug 11 '13 at 11:42
  • @OliCharlesworth: Thanks, I didn't know that (ironically, I normally use `tag_…`; I don't know why I changed it here). While I'm aware that you can use the same name, I've always been uncomfortable doing so. I must confess, though, that it's been driven by nothing more than a vague sense that it's not a good idea. – Marcelo Cantos Aug 11 '13 at 11:45
1

With typedef keyword you can write

myType x;

and without that you have to type:

struct myType x;

Consequently, you type less and if you write big programs, this is necessary.

Seconds, some argue that if you using typedef you can hide the implementation from the users of the code. As an example you use size_t type without have to know how it is defined.

Christos Papoulas
  • 2,469
  • 3
  • 27
  • 43