存儲整數,我創建一個結構隨書properties.I讓用戶創建具有for-loop.Like書[I] Books1,Books2等結構的對象...我不能在結構
問題是我不能在結構中存儲整數值。
代碼如下。
#include <stdlib.h>
#include <stdio.h>
struct Book {
int ID[];
char book_name[80];
char author_name[50];
int pblsh_date[];
};
struct Book *Books;
void Create();
int main() {
int count;
printf("How many books do you want to enter? ");
scanf("%d", &count);
Create(count);
//Show
printf("ID\t\tName\tAuthor\tPublish Year\n");
for (int i= 0; i < count; i++)
printf("%d\t%s\t%s\t%d\n", Books[i].ID, Books[i].book_name, Books[i].author_name, Books[i].pblsh_date);
if (Books) {
free(Books);
}
getchar();
return 0;
}
void Create(int count) {
Books = (struct Book*) malloc(count * sizeof(struct Book));
int i;
for (i = 0; i < count; i++) {
printf("%d. Book's ID: ", i+1);
scanf("%d", Books[i].ID);
printf("Book's name: ");
scanf("%s", Books[i].book_name);
printf("Author: ");
scanf("%s", Books[i].author_name);
printf("Publish Year: ");
scanf("%d", Books[i].pblsh_date);
}
}
將方括號從ID和pblsh_date字段中除去。 –
謝謝你的迴應:)我在第一個地方試過,但是當我沒有使用括號時,程序崩潰了。 http://tinyurl.com/guydtnl –
由於'scanf(「%d」,Books [i] .ID);'和'scanf(「%d」,Books [i] .pblsh_date) ;'。應該是'scanf(「%d」,&(Books [i] .ID));'和'scanf(「%d」,&(Books [i] .pblsh_date));' –