user3277837
user3277837

Reputation:

C Simulate Pass-By-Reference With Array of Linked Lists

I'm working in C, trying to pass a reference of a list of linked lists, but when I compile and run my code, I am getting a segmentation fault on my printing of values.

These are my structures:

typedef struct node_{
    int value;
    /*struct node_ next;*/
    struct node_ *next; /*1) This needs to be a pointer to a node*/
}Node;

typedef Node* List;

My function prototypes:

int create_list(List*, FILE*);
void  print_list(List*, int);
Node* new_node(int);

The call of my functions:

int length = create_list(array, fp);
printf("Pre Sort\n");
print_list(array, length);

My functions in use:

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

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

    //array = malloc(sizeof(Node));
    array = malloc(sizeof(List));
    for(i = 0; i < length; i++)
    {
        //(*array)[i] = NULL;
        array[i] = NULL;
    }
    while(!feof(fp)){
        fscanf(fp, "%d %d", &index, &value);

        Node* node = new_node(value);

        node->next = array[index];
        array[index] = node;
    }
}

/*Creates a new node of type Node, sets node->value = value and returns it. */
Node* new_node(int value){
    Node* node;
    node = malloc(sizeof(Node));
    node->value = value;
    node->next = NULL;

    return node;
}

/*For each index in the array, print the index, the linked list it points to,
 * and the sum of its nodes. See the sample output for an example.*/
void print_list(List* array, int length){
    int i;
    for(i = 0; i < length; i++){
        Node* curr = array[i];
        printf(" -\n|%d|", i);
        while(curr->next != NULL)
        {
            printf("%d ->", curr->value);
            curr = curr->next;
        }
        printf("NULL = %d\n -\n", list_sum(array[i]));
    }
}

Any reason why it would not print correctly? It prints the values if I try to do any form of print within the create_list() function itself.

Upvotes: 0

Views: 254

Answers (1)

M Thotiger
M Thotiger

Reputation: 344

There are some issues in the code , Please check

  1. create_list(List* array, FILE* fp), In this function if you are allocating memory for
    array then it will not reflect back in the caller, because it is taken as single pointer.

    assuming you have defined as array as "list *array", so it should be changed to take double pointer create_list(List** array, FILE* fp) and should be invoked as below

    create_list(&array, fp);

  2. array = malloc(sizeof(List)); , In this it will allocate only 4 bytes for array (considering you are running on 32 bit machine) since the size of pointer is 4 bytes

    so the the following lead to segmentation fault

    for(i = 0; i < length; i++)
    {
       //(*array)[i] = NULL;
       array[i] = NULL;
    }
    

    Change the allocation to array code as below:

    array = malloc(sizeof(list) * length);

  3. List construction is also not , why are taking the index also ? What is the purpose of this index value ?

    without the index list can be constructed as ( Here allocation to the array is also not needed)

    list *head = NULL;

    while(!feof(fp)) {

    fscanf(fp, "%d %d", &index, &value);
    
    Node* node = new_node(value);
    if(NULL == node)
    {
       //error , you can return from here
    }
    
    
    if(NULL == list)
    {
       head = node;
    }
    else
    {
        node->next = head;
        head = node;
    }
    

    }

    *arrary = head;

Upvotes: 1

Related Questions