Generic C linkedlist compilation erros

I am trying to write a generic linked list in c, but for some reason i keep getting errors saying "incompatible pointer type. This is the code and erros:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef SLIST_H
#define SLIST_H 

#include <stdlib.h>

typedef struct {
	void *data;
	slist_elem *next;
} slist_elem;

typedef struct {
	int size;
	slist_elem *head;
	slist_elem *tail;	
} slist;


void slist_init(slist *list);


void destory(slist *list);


void insert(slist *list, const void *data);


void remove(slist *list, slist_elem *elem, int (*compare)(void *data, void *olddata));


int list_size(slist *list);

#endif /* slist.h */ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "slist.h" 

void slist_init(slist *list)
{
	list->size = 0;
	list->head = NULL;
	list->tail = NULL;
}


void destory(slist *list)
{
	slist_elem *lptr = list->head;
	while(lptr) {
		free(lptr->data);
		lptr = lptr->next;
	}
	list->size = 0;
	list->head = NULL;
	list->tail = NULL;
}

/* inserts elements at the tail of the list */
void insert(slist *list, const void *data)
{
	slist_elem *element = (slist_elem *)malloc(sizeof(slist_elem));
	element->data = (void *)data;
	element->next = NULL;

	if(!list->head && element) { //empty list
		list->head = element;
		list->tail = element;
	} else {
		list->tail->next = element;
	}
}

void remove(slist *list, slist_elem *elem, int (*compare)(void *data, void *olddata))
{
	if(!list) //NULL list
		return;

	else if(!compare(list->head, elem->data)) { //match was at head
		slist_elem *lptr = list->head->next;
		free(list->head->data);
		list->head = lptr;
		return;
	}

	else {
		slist_elem *lptr = list->head;

		while(lptr->next != list->tail) {
			if(!compare(lptr->next->data, elem->data)) {//found a match
				free(lptr->next->data);

				if(lptr->next != list->tail)
					lptr->next = lptr->next->next;
				else
					lptr->next = NULL; //if item deleted was tail
			} else {
				lptr = lptr->next;
			}
		}
	}
}


int list_size(slist *list)
{
	int size = 0;
	slist_elem *lptr = list->head;

	while(lptr->next) {
		size++;
		lptr = lptr->next;
	}
	return size;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
OUTPUT from GCC

slist.h:25:2: error: unknown type name ‘slist_elem’
  slist_elem *next;
  ^
slist.c: In function ‘destory’:
slist.c:33:8: warning: assignment from incompatible pointer type [enabled by default]
   lptr = lptr->next;
        ^
slist.c: In function ‘insert’:
slist.c:51:20: warning: assignment from incompatible pointer type [enabled by default]
   list->tail->next = element;
                    ^
slist.c: In function ‘remove’:
slist.c:61:22: warning: initialization from incompatible pointer type [enabled by default]
   slist_elem *lptr = list->head->next;
                      ^
slist.c:70:20: warning: comparison of distinct pointer types lacks a cast [enabled by default]
   while(lptr->next != list->tail) {
                    ^
slist.c:71:26: error: request for member ‘data’ in something not a structure or union
    if(!compare(lptr->next->data, elem->data)) {//found a match
                          ^
slist.c:72:20: error: request for member ‘data’ in something not a structure or union
     free(lptr->next->data);
                    ^
slist.c:74:19: warning: comparison of distinct pointer types lacks a cast [enabled by default]
     if(lptr->next != list->tail)
                   ^
slist.c:75:29: error: request for member ‘next’ in something not a structure or union
      lptr->next = lptr->next->next;
                             ^
slist.c:79:10: warning: assignment from incompatible pointer type [enabled by default]
     lptr = lptr->next;
          ^
slist.c: In function ‘list_size’:
slist.c:93:8: warning: assignment from incompatible pointer type [enabled by default]
   lptr = lptr->next;
        ^


Thanks for the help.
I figured it out. I had to change
1
2
3
4
5
6
7
8
9
10
typedef struct {
	void *data;
	slist_elem *next;
} slist_elem;

typedef struct {
	int size;
	slist_elem *head;
	slist_elem *tail;	
} slist;


To this:
1
2
3
4
5
6
7
8
9
10
typedef struct {
	void *data;
	struct slist_elem *next;
} slist_elem;

typedef struct {
	int size;
	struct slist_elem *head;
	struct slist_elem *tail;	
} slist;
Last edited on
Ok thanks. I edited the post before I saw your reply. Thank you for the help
Topic archived. No new replies allowed.