c and c++ programming difference-IV

@angnima (772)
Nepal
March 3, 2007 11:54pm CST
c and c++ programming difference-IV: Structs and Enums: You have to include the struct keyword before the name of the struct type to declare a struct: In C++, you could do this struct a_struct { int x; }; a_struct struct_instance; and have a new instance of a_struct called struct_instance. In C, however, we have to include the struct keyword when declaring struct_instance: struct a_struct struct_instance; In fact, a similar situation also holds for declaring enums: in C, you must include the keyword enum; in C++, you don't have to. As a side note, most C programmers get around this issue by using typedefs: typedef struct struct_name { /* variables */ } struct_name_t; Now you can declare a struct with struct_name_t struct_name_t_instance; But there is another gotcha for C++ programmers: you must still use the "struct struct_name" syntax to declare a struct member that is a a pointer to the struct. typedef struct struct_name { struct struct_name instance; struct_name_t instance2; /* invalid! The typedef isn't defined yet */ } struct_name_t;
No responses