Hi
I am having problems comiling the following code
List Header file
// list.h - Linked List ADT
Program to insert info into listCode:#ifndef _LIST_H_ #define _LIST_H_ struct List; typedef struct ListNode *Position; List *ListConstruct(); // NULL if fails void ListDestroy(List *l); bool ListEmpty(const List *l); Position ListHead(const List *l); Position ListFirst(const List *l); Position ListNext(const List *l, Position p); Position ListLast(const List *l); Position ListAdvance(const List *l, Position p); Position ListPrevious(const List *l, Position p); void *ListRetrieve(const List *l, Position p); bool ListInsert(List *l, Position p, void *item); void ListDelete(List *l, Position p); typedef void ListCallback(const void *); void ListTraverse(const List *l, ListCallback *fn, bool fwd); #endif
I am getting the following compile errorsCode:#include <stdlib.h> #include <stdio.h> #include "list.h" //the callback function void printname(void *ptr) { printf("%s\n", (char *)ptr); } int main() { List *l; Position p; if ((l = ListConstruct()) == NULL) { exit(EXIT_FAILURE); //unable to create list } p = ListHead(l); // start at front of enpty list //insert an item at the front if (!ListInsert(l,p, "Freddy")) { exit(EXIT_FAILURE); } //insert another item after that - move position first p = ListNext(l,p); if (!ListInsert(l, p, "Billy")) { exit(EXIT_FAILURE); } //now insert a third one before the last if (!ListInsert(l, p, "Johnny")) { exit(EXIT_FAILURE); } //Finally the last one at the end p=ListLast(l); if (!ListInsert(l, p, "Mickey")) { exit(EXIT_FAILURE); } // Now print out the list in the forward direction ListTraverse(l, printname, true); //destroy the list before finishing ListDestroy(l); return 0; }
namelists.c: In function `int main()':
namelists.c:20: invalid conversion from `const void*' to `void*'
namelists.c:26: invalid conversion from `const void*' to `void*'
namelists.c:31: invalid conversion from `const void*' to `void*'
namelists.c:37: invalid conversion from `const void*' to `void*'
namelists.c:43: invalid conversion from `void (*)(void*)' to `void (*)(const void*)'
I am assuming the reason for this is something to do with pointers, but I am at a bit of loss as to what to do as we haven't been provided with many examples.
Could someone explain why I am getting this error
Thanks
PT



LinkBack URL
About LinkBacks



When you assign pointers, you do just that, assign the pointer. The memory the pointers refer to remains the same, so you have two pointers that refer to the same memory. If your driver looked like this then all of your nodes would have the same contents because they all point to buffer: