Reputation: 107
I am a beginner in C language, so I was seeking for some projects. And I found a project named bank management system. I went through the code and find this weird struct
. Can anybody explain me this code snippet and how to use this kind of structs.
struct {
char name[60];
int acc_no,age;
char address[60];
char citizenship[15];
double phone;
char acc_type[10];
float amt;
struct date dob;
struct date deposit;
struct date withdraw;
}add, upd, check, rem, transaction; // What are those names? Are they several names for the `struct`?
And also this struct
is in a function called menu. Can we use this struct
in another function without passing it to function?
Upvotes: 1
Views: 92
Reputation: 134316
Here add
, upd
, check
, rem
, transaction
are all variables of that structure type. The structure itself has no name, also known as unnamed struct.
To elaborate, the syntax for a structure declaration is:
struct-or-union-specifier:
struct-or-union identifier(opt) { struct-declaration-list }
where you can see the identifier is optional. So we can have unnamed structs.
Upvotes: 2
Reputation: 137
As Sourav explained, add
et al. are variables of type struct. This is usually discouraged if more definitions are required of this type.
If re-use of structs are intended, then a name needs to be mentioned while defining the struct, like the following:
struct foo {
int a; etc.
} add, upd;
and then later,
struct foo check;
The scope of this definition is the same scope it is defined in. As you mentioned it is declared in a function, the scope of this is only that function.
Upvotes: 1