soulkingbrook
soulkingbrook

Reputation: 81

Segfault when implementing a linked list with an array in C

Alrighty, first off, I am 100% positive that it is not my print function that is messing up this program, but my output is printing "pre" and then segfaulting. I believe that it's happening within my create_list function. My logic in that function is that the array (the linked-list typedef is Node, so head is Node*, the array that holds the heads is Node**) holds the heads of several different linked-lists, and stores each branch according to the index (first number in the input). But obviously my logic in programming isn't equal to what I'm thinking. Any assistance would be great, thanks.

int main(int argc, char *argv[]){

    if ( argc != 2 ) {
        printf("Insufficient arguments.\n");
        return 0;
    }

    FILE* fp = fopen(argv[1], "r"); 
    printf("here");
    while(fp == NULL){
        char file[MAX_FILE_LENGTH];
        printf("Unable to open file, enter a new file name: ");
        scanf("%s", file); 
        fp = fopen(file, "r");
    }
    Node** array = NULL; 
    int length = create_list(array, fp);

    fclose(fp); 

    printf("pre\n");

    print_list(array, length);

    return 0;

    }
int create_list(Node** array, FILE* fp){ 
    int length, i, index, value;

    fscanf(fp, "%d\n", &length); 

    array = malloc(sizeof(Node*)*length); //allocate memory for the pointers

    for(i = 0; i < length; i++){

        array[i] = NULL; //set all the pointers to null
    }

    while ( !feof(fp) ) //until it reaches eof
    {

        fscanf(fp, "%d %d\n", &index, &value);

        Node* node = new_node(value); //get the node

        if ( array[index] == NULL ) { //if nothing is in there yet at the index

            array[index] = node; //make whatever is at the index this node

        }

        else { //otherwise
            Node* head = array[index]; //head equals the thing
            while ( head->next != NULL ) { //go through the list until next is null
                head = head->next;
            }
            head->next = node; //then make that null next point to the new node

        }

    }

    return length;
}

void print_list(Node** array, int length){
    int i;
    for(i = 0; i < length; i++){
        Node* curr = array[i]; //make the head what's stored in the array

        printf(" %d ", i); //index

        printf("%d ->", curr->value); //print the value

        curr = curr->next; //move it
    }
}

Upvotes: 0

Views: 131

Answers (1)

Klas Lindb&#228;ck
Klas Lindb&#228;ck

Reputation: 33273

One problem is here:

Node** array = NULL; 
int length = create_list(array, fp);

Parameters are passed by value, which means that you pass NULL to create_list, and array will still be NULL when create_list returns.

There are several ways to fix this. For example like this:

Node** array = NULL; 
int length = create_list(&array, fp);

And:

int create_list(Node*** arrayp, FILE* fp){ 
    int length, i, index, value;
    Node **array;

    fscanf(fp, "%d\n", &length); 

    array = *arrayp = malloc(sizeof(Node*)*length); //allocate memory for the pointers

Upvotes: 1

Related Questions