This is a follow up to how to create a function pointer to add a print
method, Adding a function pointer within a struct to print. Let's say I've created this as follows:
typedef struct Book {
char* title;
unsigned int year;
void (*print)(struct Book *book);
} Book;
void print(Book *book)
{
printf("{\n\ttitle: \"%s\",\n\tyear: %d\n}\n", book->title, book->year);
}
int main(int argc, char * argv[])
{
Book book = {
.title="Jaws",
.year=2000
};
print(&book); // this works
book.print(&book); // this seg faults
}
What would be the proper way to do the book.print()
call?