Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using struct keyword in variable declaration in C++

Tags:

c++

struct

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 */         }; 
like image 312
void.pointer Avatar asked Oct 11 '11 16:10

void.pointer


People also ask

How do you declare a struct variable?

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.

How do you declare a struct in C?

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.

What is the use of struct keyword in C?

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

How do you use struct keywords?

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.


2 Answers

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

like image 169
David Rodríguez - dribeas Avatar answered Sep 29 '22 00:09

David Rodríguez - dribeas


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; } 
like image 29
pmg Avatar answered Sep 28 '22 23:09

pmg