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?
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 */ };
You begin a structure declaration with the Structure Statement, and you end it with the End Structure statement. Between these two statements you must declare at least one element. The elements can be of any data type, but at least one must be either a nonshared variable or a nonshared, noncustom event.
The general syntax for a struct declaration in C is: struct tag_name { type member1; type member2; /* declare as many members as desired, but the entire structure size must be known to the compiler. */ }; Here tag_name is optional in some contexts.
Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).
struct is used to declare a new data-type. Basically this means grouping variables together. For example, a struct data type could be used to declare the format of the following file.
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
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With