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;
}
  • 2
    `lib->num_books = 0;` ? – Soner from The Ottoman Empire Jul 31 '15 at 03: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. – kaylum Jul 31 '15 at 03:46
  • Yeah, 'lib' pointer not initialized:( – Martin James Jul 31 '15 at 03:48
  • The issue seems to be where the FILE pointer is located because once removed the program runs fine. – Leopold_Stotch Jul 31 '15 at 03:52
  • @Leopold_Stotch No it's not. The previous comments have already told you where the error is - uninitialised `lib` variable. Accessing an unintialised variable is [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). By removing the `FILE` pointer you just shift the behaviour (which is still undefined) so that it no longer crashed. But the underlying problem is unresolved by that. – kaylum Jul 31 '15 at 03:56
  • I initialized lib globally and was still receiving the error. – Leopold_Stotch Jul 31 '15 at 03:59
  • What do you mean by "initialized globally", if it's a local variable? – Dan Getz Jul 31 '15 at 04:02
  • 1
    Update the code in your question with that initialisation. Otherwise all the answers will be about that unitialised variable. – kaylum Jul 31 '15 at 04:04
  • You need [`valgrind`](http://valgrind.org). Fix the *very first* error it tells you about, ignore the rest of them, recompile, retest, repeat till program works correctly. – zwol Jul 31 '15 at 04:10
  • 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. – kaylum Jul 31 '15 at 04:11
  • Also, [never, ever use `scanf` for anything](https://stackoverflow.com/questions/15664664/scanf-regex-c/15664816#15664816). This isn't your immediate problem but it will probably be your next one. – zwol Jul 31 '15 at 04:12
  • To be explicit. You need to allocate memory for `lib`. So get rid of the global one and change the local one to be: `struct library *lib = malloc(sizeof *lib);` – kaylum Jul 31 '15 at 04:13
  • Also, even if you finally manage to initialize `lib`, you're going to run into the fact that `Book` is `NULL`, right? Why are `lib` and `Book` pointers to begin with? It seems like the way you're using them, a local non-pointer variable would work fine. Perhaps you're not clear on the difference between a `struct` value and a pointer to it? – Dan Getz Jul 31 '15 at 04:14

2 Answers2

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.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
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.

Santosh Gupta
  • 91
  • 1
  • 8