I have a C struct defined as follows:
struct Guest {
int age;
char name[20];
};
When I created a Guest
variable and initialized it using the following:
int guest_age = 30;
char guest_name[20] = "Mike";
struct Guest mike = {guest_age, guest_name};
I got the error about the second parameter initialization which tells me that guest_name
cannot be used to initialize member variable char name[20]
.
I could do this to initialize all:
struct Guest mike = {guest_age, "Mike"};
But this is not I want. I want to initialize all fields by variables. How to do this in C?
std::string
.std::string
. But what if I want to stick with C-style char arrays?