C
cp
I'm not an excellent C-programmer so forgive possible, in-your-eyes, stupid
mistakes below.
struct book {
char name[20];
char genre[20];
int serial_number;
}
struct book * insert_book(char bookname[20], char bookgenre[20], int
booknumber) {
struct book * book_ptr;
book_ptr = malloc(sizeof(struct book));
if(book_ptr == NULL){
printf("Memory not allocated\n");
exit(-1);
}
(*book_ptr).name[20] = bookname[20];
(*book_ptr).genre[20] = bookgenre[20];
(*book_ptr).serial_number = booknumber;
printf("Completed\n");
return(book_ptr);
}
int main(void){
struct book * insert_book_ptr;
insert_book_ptr = insert_book("Hello World", "horror", 123);
printf("bookname is %s, genre is %s and number is
%d\n",(*insert_book_ptr).name, (*insert_book_ptr).genre,
(*insert_book_ptr).serial_number);
}
When I compile it (gcc 4.03) i am informed that:
c:11: Error: "two or more data types in declaration specifiers"
Line 11 is: struct book * insert_book(char bookname[20], char bookgenre[20],
int booknumber) {
What am i doing wrong?
mistakes below.
struct book {
char name[20];
char genre[20];
int serial_number;
}
struct book * insert_book(char bookname[20], char bookgenre[20], int
booknumber) {
struct book * book_ptr;
book_ptr = malloc(sizeof(struct book));
if(book_ptr == NULL){
printf("Memory not allocated\n");
exit(-1);
}
(*book_ptr).name[20] = bookname[20];
(*book_ptr).genre[20] = bookgenre[20];
(*book_ptr).serial_number = booknumber;
printf("Completed\n");
return(book_ptr);
}
int main(void){
struct book * insert_book_ptr;
insert_book_ptr = insert_book("Hello World", "horror", 123);
printf("bookname is %s, genre is %s and number is
%d\n",(*insert_book_ptr).name, (*insert_book_ptr).genre,
(*insert_book_ptr).serial_number);
}
When I compile it (gcc 4.03) i am informed that:
c:11: Error: "two or more data types in declaration specifiers"
Line 11 is: struct book * insert_book(char bookname[20], char bookgenre[20],
int booknumber) {
What am i doing wrong?