I have a feeling this may be related to C syntax, but I started my programming life with C++ so I am not sure.
Basically I have seen this:
struct tm
The user-defined types have their own identifier space, i.e. when the compiler parse the file it stores each identifier in its corresponding space.
When you refer to tm
, the C compiler (as the C++ one) will search for this identifier in the global identifier space. The C++ compiler will then lookup in the user-defined types identifier space if it hadn't found the symbol before.
Basically, if you want to have the same behavior as in C++, add this line :
typedef struct tm tm;
You can combine struct declaration and typedef like that :
typedef struct tm { int field } tm;
Or using a anonymous struct :
typedef struct { int field } tm;
The same behavior applies for enum
and union
:
typedef enum { VALUE } myEnum;
typedef union { int integer; char charArray[4]; } myUnion;
In C, the struct tag names do not form identifiers on the global name space
struct not_a_global_identifier { /* ... */ };
To refer to that struct you have to use the keyword struct
(to specify the name space)
struct not_a_global_identifer object;
or create a new identifier, in the global name space, with typedef
typedef struct not_a_global_identifer { /* ... */ } global_name_space_identifier;
There are 4 namespaces in C, see 6.2.3 in the C99 Standard:
This is a legal C program :-)
int main(void) {
typedef struct foobar { int foobar; } foobar;
foobar boo;
boo.foobar = 42;
if (boo.foobar) goto foobar;
foobar:
return 0;
}
The simple answer is that the struct
keyword there is present to restrict the lookup of the identifier tm
to only user defined class types. It is probably left for compatibility with C, where it is required.
Contrary to what others say, there is no such thing as auto-typedef, nor do C and C++ differ with respect to how the identifiers for user defined types are managed. The only difference is in lookup.
You can read more here
Using struct tm t;
is for compatibility with C, in which declaring a struct named "tm
" defines a type named "struct tm
" but not one named "tm
" (as opposed to C++, in which both names for the type are declared).
You can see the reference linked below and quoting from there "In C, you must explicitly use the struct keyword to declare a structure. In C++, this is unnecessary once the type has been defined." See the link for more information and examples.
http://msdn.microsoft.com/en-us/library/64973255%28v=vs.80%29.aspx
In your example tm can have been a typecasted structure.
e.g.
typedef struct tm_t
{
int x;
}tm;
and then you can do
tm t;