I am aware of two possible ways to define and use structs:
#1
struct person
{
char name[32];
int age;
};
struct person dmr = {"Dennis Ritchie", 70};
#2
typedef struct
{
char name[32];
int age;
} person;
person dmr = {"Dennis Ritchie", 70};
The interesting property of the first way is that both the type and the variable can have the same name:
struct person person = {"Sam Persson", 50};
Is that idiomatic in C? Is it guaranteed to work in C++? Or are there corner cases I should be aware of?
Note that I am not interested in pure C++ answers (e.g. “use std::string instead of char[32]“). This is a question about C/C++ compatibility.
It is not idiomatic to use the same identifier for a type and a variable. In Unix, it is typical to use an abbreviated version for the variable name, e.g:
struct stat st,struct timeval tv, … the same wayfdis a typical name for a file descriptor variable.