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.
 
     
     
     
     
    