Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set struct pointer to NULL with function

Tags:

c

pointers

struct

I am new to C. I set up 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 struct by use clear(struct pointer). But in last line of clear(), I got this error:

error: incompatible types in assignment

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

Thanks! @Marievi I've changed code like this:

headNode = NULL;

But when I want to print the linked list. It 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 called this function, it seems leak.

like image 620
桂小方 Avatar asked Jan 19 '26 13:01

桂小方


2 Answers

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

 headNode = NULL;

If you want to assign NULL to the 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 the 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);
like image 99
0___________ Avatar answered Jan 21 '26 06:01

0___________


Change line :

*headNode = NULL;

to :

headNode = NULL;

and the error will disappear.


Also, do not cast the result of malloc and always check if malloc was successful.

like image 31
Marievi Avatar answered Jan 21 '26 04:01

Marievi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!