using struct keyword in variable declaration in C++

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 t;
memset( &t, 0, sizeof(struct tm) );

I am a bit confused with this syntax, as normally I would expect the above to instead look like this:

tm t;
memset( &t, 0, sizeof(tm) );

What is the difference between the two, and why is the former used instead?

Update

The structure tm that I am referring to is in wchar.h, and the definition is as follows:

struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };

Solution 1:

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

Solution 2:

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:

  • label names
  • the tags of structures, unions, and enumerations
  • the members of structures or unions (not a single name space ... as many as structures or unions are defined)
  • global name space, for all other identifiers

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;
}

Solution 3:

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).

Solution 4:

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;

Solution 5:

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;