1

I am just wondering out of curiosity, why is in C time structure

struct tm

used as this and not with typedef. This way I always need to write struct tm, to have a correct code. A lot of structures are "typedefed", why not this one?

Martin Perry
  • 9,232
  • 8
  • 46
  • 114

4 Answers4

5

Because it would be rather rude to pollute the general namespace with an unnecessary typedef when the language has a separate namespace for struct tags. If you really want a typedef to hide the fact that it's a struct, you can easily provide one yourself without forcing it on the rest of us.

A lot of structures are "typedefed", why not this one?

I don't know why you say that; there are no typedefed structs in the C library. FILE is a typedef, but not necessarily a struct.

Maybe you mean that there are a lot in other bodies of code. That's because some people, like yourself, think that namespace pollution is a good idea for some reason. The C library authors don't.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

I think the thing is that in C this structure was born this way!

You are free to create your include file that declare the tm typedef, or declare it in your code!

typedef struct tm tm;

or better (on my opinion)

typedef struct tm tm_t;
Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22
  • 1
    [`_t` is reserved by POSIX](http://stackoverflow.com/a/3225396/1366431); using it is inadvisable. Subjectively, many people also find it unpleasant style (if you need to mark a word out as referring to a type, your naming scheme sucks). – Alex Celeste Apr 12 '15 at 12:26
  • Then you say that: time_t, size_t, ssize_t are unadvisable!!! ... I use _t since 25 years ... I think (I know) posix uses _t to specify just that a typedef is (often) used: typedef unsigned long int pthread_t; typedef union { [...]; } pthread_mutex_t; ... and so on ... Give a look at pthreadtypes.h! – Sir Jo Black Apr 12 '15 at 12:43
  • ... grep -i "_t;" stdio.h – Sir Jo Black Apr 12 '15 at 13:08
  • Yep. There's no reason for the standard's conventions to apply to user-written code. It has cultural baggage; we don't. – Alex Celeste Apr 12 '15 at 13:24
  • I think different, I think that is good idea, above all if you are writing libraries, to indicates the types of the "object" you manage! This increase the readability of the code! The naming of the element of your code is important as the code! – Sir Jo Black Apr 12 '15 at 13:27
  • It does nothing to increase readability of the code whatsoever (in fact it hurts it by putting zero-information boilerplate on screen). That the name refers to a type is visible from the usage in C. Use that space to actually tell the reader something *about* the type. – Alex Celeste Apr 12 '15 at 14:17
  • Free to have your ideas!!! ... I fill more simple to identify immediately the context of which I see! Moreover, I don't agree the community declares as standard POSIX stuffs that are not indicated in ufficial POSIX documents... !!! – Sir Jo Black Apr 12 '15 at 16:01
0

Because C++ is different with C. C++ is not superset of C and it is just language specification.

Mr. Handy
  • 356
  • 2
  • 14
0

You can use a #define line written on the top of your code to help you to write faster.

#define TM struct tm

Then, instead of struct tm write TM.

webpersistence
  • 894
  • 3
  • 12
  • 29