-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookPointer.cpp
62 lines (50 loc) · 1.58 KB
/
BookPointer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Defining book structure
struct Book {
char* title;
char* author;
int pageNumber;
};
// Function that creates a new book
struct Book* createNewBook(const char* title, const char* author, int pageNumber) {
// Allocate space from memory
struct Book* newBook = (struct Book*)malloc(sizeof(struct Book));
// If memory allocation failed
if (newBook == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
// Allocate memory for title and author
newBook->title = (char*)malloc(strlen(title) + 1); // +1: for null
newBook->author = (char*)malloc(strlen(author) + 1); // +1: for null
// If memory allocation failed
if (newBook->title == NULL || newBook->author == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
// Copy the information
strcpy(newBook->title, title);
strcpy(newBook->author, author);
newBook->pageNumber = pageNumber;
return newBook;
}
// Function that prints the book to the screen
void printBook(struct Book* book) {
printf("Title of the book : %s\n", book->title);
printf("Author : %s\n", book->author);
printf("Page Number : %d\n", book->pageNumber);
}
int main(int argc, char const *argv[])
{
// Create a new book
struct Book* newBook = createNewBook("Sefiller", "Victor Hugo", 1463);
// Print the book on screen
printBook(newBook);
// Release the allocated space from memory
free(newBook->title);
free(newBook->author);
free(newBook);
return 0;
}