Reputation: 8778
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?
Upvotes: 1
Views: 3665
Reputation: 18436
You must use strcpy (if you know the length of the input) or a safe function instead.
A lot of the other answers made the same mistake of leaving un-terminated strings, a major source of security vulnerabilities.
The correct way is to use a safe string copy function, like StringCbCopy or roll your own (albeit not as robust):
// Copy at most n-1 characters to dst, truncating if strlen(src) > n-1
// and guaranteeing NUL-termination.
void safe_strcpy(char *dst, const char *src, size_t n) {
strncpy(dst, src, n-1);
dst[n-1] = 0; // Guarantee NUL-termination.
}
Then you may use it as follows
void f(const char *title, const char *author) {
BookType book;
safe_strcpy(book.title, title, sizeof book.title);
safe_strcpy(book.author, author, sizeof book.author);
}
Upvotes: 2
Reputation: 12524
void InitBookStruct(BookType *book, const char *title, const char *author){
size_t title_length = sizeof book->title;
size_t author_length = sizeof book->author;
strncpy(book->title, title, title_length - 1); //-1, make way for null byte
strncpy(book->author, author, author_length - 1);
book->title[title_length - 1] = 0;
book->author[author_length - 1] = 0;
}
Many ways to do, above is one of them.
From man pages,
char *strncpy(char *dest, const char *src, size_t n);
If the length of src is less than n, strncpy() pads the remainder of dest with null bytes.
So, specifying (one less than) the size of the dest
is sufficient.
Upvotes: 2
Reputation: 208323
There are two possible solutions to your problem. The first of which is using the string literals in the place of construction:
BookType book = { "A book on C", "A. Kelly" };
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:
BookType book = { 0 }; // 0 initalize
strncpy( book.title, title, sizeof(book.title)-1 );
strncpy( book.author, author, sizeof(book.author)-1 );
Upvotes: 5
Reputation: 1952
If you change your structure to this it should work
typedef struct bookStruct
{
char* title;
char* author;
} BookType;
Upvotes: 1
Reputation:
As far as I'm aware, there is no way of doing that in C. Your best bet is probably to use a macro:
#define TITLE "A Book On C"
#define AUTHOR "A. Kelley"
BookType book {TITLE, AUTHOR};
though this of course does not have exactly the same effect.
Upvotes: 1