| structures/books-alt.c - En variation der allokerer de tre tekststrenge inde i struct book. | Lektion 12 - slide 11 : 36 Program 2 |
#include <stdio.h>
#include <string.h>
#define TITLE_MAX 50
#define AUTHOR_MAX 40
#define PUBLISHER_MAX 20
struct book {
char title[TITLE_MAX], author[AUTHOR_MAX], publisher[PUBLISHER_MAX];
int publishing_year;
int university_text_book;
};
typedef struct book book;
book make_book(char *title, char *author, char *publisher,
int year, int text_book){
book result;
strcpy(result.title, title); strcpy(result.author, author); strcpy(result.publisher, publisher);
result.publishing_year = year;
result.university_text_book = text_book;
return result;
}
void prnt_book(book b){
char *yes_or_no;
yes_or_no = (b.university_text_book ? "yes" : "no");
printf("Title: %s\n"
"Author: %s\n"
"Publisher: %s\n"
"Year: %4i\n"
"University text book: %s\n\n",
b.title, b.author, b.publisher,
b.publishing_year, yes_or_no);
}
int main(void) {
book b1, b2, b3;
b1 = make_book("Problem Solving and Program Design in C", "Hanly & Koffman",
"Pearson", 2010, 1);
b2 = make_book("Pelle Erobreren", "Martin Andersen Nexoe",
"Gyldendal", 1910, 0);
b3 = b2;
strcpy(b3.title, "Ditte Menneskebarn"); b3.publishing_year = 1917;
prnt_book(b1); prnt_book(b2); prnt_book(b3);
printf("Sizeof(b1) = %d bytes\n", sizeof(book)); /* 120 bytes */
return 0;
}