so I've got a struct called 'library' that stores objects of the struct 'books', and is initialized by a list of 3 books, but when I try to print the object's attributes I get a "Segmentation fault (core dumped)" error. I understand that it means I'm trying to access some memory I don't have access to, but in this case I can access the first element correctly, so it makes me believe I initialized something incorrectly.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXBOOKS 10
typedef struct books
{
char* title;
char* author;
int id;
}book;
typedef struct library
{
int number_of_books;
book* booklist [MAXBOOKS];
}lib;
void storeBook(lib library,book CurrentBook)
{
library.booklist[0] = &CurrentBook;
}
void printLibrary(lib library)
{
for(int i = 0; i < library.number_of_books; i++)
{
printf("Author:%s\n",library.booklist[i]->title);
}
}
int main()
{
book b_1 = {"The trial","Kafka",101};
book b_2 = {"The lurking fear","Lovecraft",102};
book b_3 = {"Dora's storytime collection","Valdes",103};
book* list = (book*)malloc(3*sizeof(book));
list[0] = b_1; list[1] = b_2; list[2] = b_3;
lib CurrentLibrary = {3,{list}};
printLibrary(CurrentLibrary);
return 0;
}
booklist1[i] = *(booklist1+i)
,thenbooklist2[i][j] = *(*(booklist2+i)+j)
, if j=0
, then *(*(booklist2+i)+j) = *(*(booklist2+i)+0) = *(booklist2[i]) = *booklist2[i]
booklist2[0]
points to first row, booklist2[1]
points to seconds row, and so,... on.
You are defining an array of book pointers (2D array) : book* booklist [MAXBOOKS]
But list
is an array of book (1-D array). After execution this statement, lib CurrentLibrary = {3,{list}};
list
array will be stored into booklist[0]
row. But all other pointers of booklist[1], booklist[2],..... booklist[9]
are not pointing to any element.
But, you are accessing booklist[1]
, booklist[2]
, and booklist[3]
in the printLibrary
function. This is the reason for Segmentation fault.
For more insight (for 2-D array), please print the following lines:
printf("Title %s\n", library.booklist[0][0].title);
prints--> Title The trial
printf("Title %s\n", library.booklist[0][1].title);
prints--> Title The lurking fear
printf("Title %s\n", library.booklist[0][2].title);
prints-->Title Dora's storytime collection
But trying to access, library.booklist[1][0].title
will throw segmentation fault, since second row pointer is not pointing to any element.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXBOOKS 10
typedef struct books
{
char* title;
char* author;
int id;
}book;
typedef struct library
{
int number_of_books;
book *booklist; // It should be 1-D array, since you are passing 1-D array of book
}lib;
void storeBook(lib library,book CurrentBook)
{
library.booklist[0] = CurrentBook;
}
void printLibrary(lib library)
{
for(int i = 0; i < library.number_of_books; i++)
{
printf("Title:%s\n",library.booklist[i].title);
printf("Author:%s\n",library.booklist[i].author);
printf("Book ID:%d\n",library.booklist[i].id);
}
}
int main()
{
book b_1 = {"The trial","Kafka",101};
book b_2 = {"The lurking fear","Lovecraft",102};
book b_3 = {"Dora's storytime collection","Valdes",103};
book* list = malloc(3*sizeof(book));
list[0] = b_1; list[1] = b_2; list[2] = b_3;
lib CurrentLibrary = {3,list}; // list is 1-D array of book
printLibrary(CurrentLibrary);
return 0;
}
The output :
Title:The trial
Author:Kafka
Book ID:101
Title:The lurking fear
Author:Lovecraft
Book ID:102
Title:Dora's storytime collection
Author:Valdes
Book ID:103
Thank you so much. But why does {book* booklist [MAXBOOKS];} define a 2D array?
book* booklist [MAXBOOKS]
is an array of 10 pointers. Find about array of pointers.Read this article: google.com/amp/s/www.geeksforgeeks.org/…
I don't see your assertion of 2D array, as it is an array of pointers to books... you can store an array or a single book... no 2D array.
@LuisColorado
book* booklist [MAXBOOKS];
is array of pointers. Please check my edit, for clear understanding.