Can I initialize a stuct
in C
in the following way:
struct database {
char* Name;
char* Title;
char* DOB;
int EmployeeNo;
} people[100];
people[0] = { "Jon", "Manager", "1-1-1990", 12345 };
people[1] = { "Bob", "Accountant", "1-1-1990", 54321 };
I am using gcc version 9.2.1 20191130 (Debian 9.2.1-21)
3
Answers
No, do it like this:
Initialization and definition must be done at the same time — otherwise, you’re assigning, not initializing.
No, the correct way is:
This is usable in C89, C90, C94, C99, C11, C17 — and pre-standard C if the initialization is done at file scope.
Not quite.
There are other ways to initialize as provided by other answers.
You can assign within a function with a compound literal.