I have a struct like this
typedef struct bookStruct
{
char title[80];
char author[80];
} BookType;
And I have two strings like this
char *title = "A Book on C";
char *author = "A. Kelly";
Now I can’t create a BookType like this
BookType book = {title, author};
Can anyone tell me what is wrong? How can I do that?
There are two possible solutions to your problem. The first of which is using the string literals in the place of construction:
In this case the compiler will copy the literals to the appropriate variables. If you cannot use the literals in the initialization, then you must copy the elements yourself: