작성
·
252
0
답변 1
0
의도하신 코드가 아래와 같이 동작하는 코드인가요?
그렇다면 아래와 같이 작성해야 하는 건 아닌지 확인해보세요.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define SLEN 101
struct book
{
char name[SLEN];
char author[SLEN];
};
void print_book(const struct book *books, int n);
int main()
{
int n;
scanf("%d", &n);
//struct book my_books[3];
struct book* my_books = (struct book*)malloc(sizeof(struct book) * n);
for (int i = 0; i < n; i++) {
scanf("%s", (my_books[i].name));
scanf("%s", (my_books[i].author));
}
if (!my_books) {
printf("malloc failed");
exit(1);
}
print_book(my_books, n);
free(my_books);
return 0;
}
//void print_book(const struct book books[], int n)
void print_book(const struct book *books, int n)
{
for (int i = 0; i < n; i++)
printf("Book %d : \"%s\" written by \"%s\"\n",
i + 1, books[i].name, books[i].author);
}