0

When debugging it, it tells me L is nullptr. I can't figure out why it doesn't returns the list properly.

These are the structs (I must use list and node):

typedef struct node node;
typedef struct List list;
struct node {
    int data;
    node *next;          
};

struct List {
    node *head;
};

The function that creates the list:

void BuildList(list *L) {
    node *head = NULL, *temp = head;
    int num;
    printf("Input list's elements: \n");
    do {
        scanf("%d", &num);
        if (num != -1) {
            if (head == NULL) {
                head = BuildNode(num);
                temp = head;
            }
            else {
                temp->next = BuildNode(num);
                temp = temp->next;
            }
        }

    } while (num != -1);

    L = (list *) malloc(sizeof(list));
    L->head = head;
}

Auxiliary function for BuildList:

node* BuildNode(int num1) {
    node *node1 = (node *)malloc(sizeof(node));

    node1->data = num1;
    node1->next = NULL;

    return node1;
}

Printing function:

void PrintList(list *L) {
    node *head;
    head = L->head;
    printf("The list's elements are: ");

    while (head != NULL) {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("\n");
}

The program fails on "head = L->head;" at PrintList, claiming it's a nullptr. It's origin is provably the dynamic allocation in BuildList, at the end. The calls from main are:

list *head = NULL;
BuildList(&head);
PrintList(head);

When replacing PrintList(head); with PrintList(&head); it prints an empty list, without failing.

2
  • Where do you ever assign to head? Commented Dec 10, 2016 at 13:12
  • which head? In BuildList or main? In BuildList head is reffered as L. Commented Dec 10, 2016 at 13:15

2 Answers 2

2

You're passing a pointer to function:

BuildList(list *L)

Which means that when you're allocating it inside of function, you will not have this pointer outside of this function, because it's on stack. What you could do is either, allocate List outside of this function like:

list *head = malloc(sizeof(list)); /* It's a good habit to not cast malloc function */ 
BuildList(head); /* Remember to remove malloc from inside of build list */
PrintList(head);

Or pass the double pointer to function:

void BuildList(list **L) {
    node *head = NULL, *temp = head;
    .....
    *L = malloc(sizeof(list));
    (*L)->head = head;
}

list *head = NULL;
BuildList(&head);
PrintList(head);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It was helful.
0

Here you need to send address of the pointer to list l as arguement not the address of list,because we want all changes done in the function to affect list l,this can be achieved only if we have the address of the list in the function,so that any changes to list are changed permanently in the memory!

so u just have to use

void BuildList(list **lreference)

and just send the address of pointer to list while calling the BuildList function.

list *l;
l = malloc(sizeof(list));
BuildFirst(&l);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.