![]() ![]() ![]() | structures/books-ptr.c - Et eksempel på overførsel og tilbageførsel af bøger via en pointer - nu OK. | Lektion 12 - slide 13 : 36 Program 4 |
#include <stdio.h> #include <stdlib.h> struct book { char *title, *author, *publisher; 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; result = (book*)malloc(sizeof(book)); if (result == NULL){ printf("Out of memory. Bye\n"); exit(EXIT_FAILURE); } result->title = title; result->author = author; 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; b1 = make_book("Problem Solving and Program Design in C", "Hanly & Koffman", "Pearson", 2010, 1); b2 = make_book("Pelle Eroberen", "Martin Andersen Nexoe", "Gyldendal", 1911, 0); prnt_book(b1); prnt_book(b2); free(b1); free (b2); return 0; }