2

Writing out this library program and I'm receiving a Segmentation Fault 11 when I run through my terminal. At first when debugging it seemed to be located somewhere with the structs but then it was giving me issues where I had my FILE pointer declared. Can someone shed some light on this matter?

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LENGTH 40 #define MAX_BOOKS 1000 struct book { char title[MAX_LENGTH]; char author[MAX_LENGTH]; char subject[MAX_LENGTH]; }*Book; struct library { struct book collection[MAX_BOOKS]; int num_books; }*lib; int main() { struct library *lib; char title[MAX_LENGTH], author[MAX_LENGTH], subject[MAX_LENGTH]; Book = NULL; lib->num_books = 0; int events = 0, selection = 0; FILE *ifp; ifp = fopen("library.txt", "r"); if (ifp == NULL) { printf("\nFile not found\n"); exit(0); } fscanf(ifp, "%d", &events); for (int i=0;i<events; i++) { Book = NULL; fscanf(ifp, "%d", &selection); switch (selection) { case 1: fgets(title, MAX_LENGTH, ifp); fgets(author, MAX_LENGTH, ifp); fgets(subject, MAX_LENGTH, ifp); strcpy(Book->title, title); strcpy(Book->author, author); strcpy(Book->subject, subject); lib->num_books += 1; //addBook(lib); break; case 2: lib->num_books -= 1; //deleteBook(); break; case 3: //search; break; case 4: break; case 5: break; default: printf("Invalid command\n"); break; } } fclose(ifp); return 0; } 
13
  • 2
    lib->num_books = 0; ? Commented Jul 31, 2015 at 3:45
  • 1
    Use a debugger. At the very least it will tell you which line of code is causing the seg fault. And used properly there's a high chance you'll spot the problem by looking at the state of the variables at the point of the seg fault. Commented Jul 31, 2015 at 3:46
  • Yeah, 'lib' pointer not initialized:( Commented Jul 31, 2015 at 3:48
  • 1
    Update the code in your question with that initialisation. Otherwise all the answers will be about that unitialised variable. Commented Jul 31, 2015 at 4:04
  • 2
    @Leopold_Stotch There's just so much wrong with what your updated code does. 1. Global variables are intialised to zero. So you still cannot dereference it. 2. You have a local variable with the same name. The local variable will hide the global one. So you are still accessing the unitialised local variable. Commented Jul 31, 2015 at 4:11

2 Answers 2

2
 lib->num_books = 0; 

This is creating problem. You have not initialized lib.

Allocate memory to it -

lib=malloc(sizeof(struct library)); 

EDIT

Also don't forget to free the allocated memory.

Sign up to request clarification or add additional context in comments.

Comments

0

1)Error is in the following line lib->num_books = 0;

The pointer variable lib is deferenced without initialization.

2) One more error is, you are opening file for read operations "r", but trying to use the file for write operations.

1 Comment

Where exactly are the write operations? Tad confused, but definitely am appreciating all of the feedback.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.