How to set struct pointer to NULL with function

I'm new to C. I created a linked list like this:

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

#define len(array) (sizeof(array) / sizeof(array[0]))

#define true 1
#define false 0

struct node{
    int data;
    struct node* next;
};

typedef struct node node;

node* initNode(void){
    node* headNode;
    headNode = (node *)malloc(sizeof(node));
    headNode -> next = NULL;
    return headNode;
}

node* create(int* array, int length){
    node* newNode;
    node* lastNode;
    node* headNode = initNode();
    newNode = (node *)malloc(sizeof(node));
    headNode -> data = array[0];
    int arrayLength = 1;
    int hasSetHead = false;
    while(arrayLength < length){
        if (! hasSetHead){
            lastNode = headNode;
            hasSetHead = true;
        }
        memset(newNode, 0, sizeof(node));
        newNode -> data = array[arrayLength];
        newNode -> next = NULL;
        lastNode -> next = newNode;
        lastNode = newNode;
        newNode = (node *)malloc(sizeof(node));
        if (newNode == NULL) exit(0);
        arrayLength++;
    }
    return headNode;
}

void clear(node* headNode){
    node* nextNode;
    if (headNode == NULL) return;
    while (headNode -> next != NULL){
        nextNode = headNode -> next;
        free(headNode);
        headNode = nextNode;
    }
    *headNode = NULL;
}

int main(void){
    int array[] = {1,2,3,4,5,6,7};
    int length = len(array);
    node* list = create(array, length);
    clear(list);
    system("pause");
    return true;
}

      

I want to clear my structure using clear (struct pointer). But on the last line of clear () I got this error:

error: incompatible types in assignment

      

What should I do? Thank! And please forget my bad English.

Thank! @Marievi I changed the code as follows:

headNode = NULL;

      

But when I want to print the linked list. Seems broken, here is my code:

void clear(node* headNode){
    node* nextNode;
    if (headNode == NULL) return;
    while (headNode -> next != NULL){
        nextNode = headNode -> next;
        free(headNode);
        headNode = nextNode;
    }
    *headNode = NULL;
}

      

When I call this function, it seems to be a leak.

+3


source to share


4 answers


This line (after removing the '*') does nothing as it only changes the local headNode parameter (and 100% is completely removed by the compiler)

 headNode = NULL;

      

If you want to assign NULL to a higher level pointer, you need to pass node ** headnode to this function, or make it node * :. 1.

void clear(node** headNode){
    node* nextNode;
    if (*headNode == NULL) return;
    while (*headNode -> next != NULL){
        nextNode = *headNode -> next;
        free(*headNode);
        *headNode = nextNode;
    }
    *headNode = NULL;
}

      

and call



 clear(&list);

      

or better

node *clear(node* headNode){
    node* nextNode;
    if (headNode == NULL) return;
    while (headNode -> next != NULL){
        nextNode = headNode -> next;
        free(headNode);
        headNode = nextNode;
    }
    return NULL;
}

      

and the call:

list = clear(list);

      

0


source


Change the line:

*headNode = NULL;

      

to:

headNode = NULL;

      



and the error will go away.


Also, do not output malloc and always check if malloc was successful .

+1


source


Given headNode

, declared in your function as

node* headNode

      

which is equivalent

struct node *headNode

      

because of the hidden typedef for the type node

. Problematic appointment

*headNode = NULL;

      

tries to assign a value NULL

, a null-type pointer constant void *

, to the object denoted *headNode

by which is struct node

. You cannot assign a pointer value to a struct type object, which is an error.

You can assign NULL

to headNode

yourself ...

headNode = NULL;

      

... as the other answer actually notes, but while the compiler will agree with this, it won't have an effect because the function parameter headNode

is local to that function. Its value is always a copy of the value (pointer) of its argument, and changes to the local copy do not affect the original caller argument.

If you want the caller copy to be modified, you have two main options:

  • Return the modified value ( NULL

    ) and let the caller assign it to the original variable if he wants to, or

  • Instead of passing a pointer to the head, pass a pointer to a variable containing a pointer to the head (type node **

    ).

Both alternatives will require changes to the caller; the latter will also require further changes in the implementation of the function.

0


source


The function is clear

too complex and wrong. It doesn't free the last item.

It is right:

void clear(node* headNode) {
  node* nextNode;

  while (headNode != NULL) {
    nextNode = headNode->next;
    free(headNode);
    headNode = nextNode;
  }
}

      

And if you want the function to clear

set a value for that argument NULL

:

Do you need it:

void clear(node** headNode) {
  node* nextNode;

  while (*headNode != NULL) {
    nextNode = (*headNode)->next;
    free(*headNode);
    *headNode = nextNode;
  }

  *headNode = NULL;
}

      

and call it like this

clear(&list);
// now list is NULL

      

0


source







All Articles