struct list_node {
int value;
struct list_node *next;
};
struct linked_list {
int size;
struct list_node *head;
};
void print_linked_list(struct linked_list *list){
struct linked_list *current = list;
while(current != NULL){
printf("%d ", current->head->value);
current = current->head->next;
}
}
I have to define a function to print out a linked list, but I get an error message saying "incompatible pointer type". I know that the problem is in "current = current->head->next;" but how can I achieve that?
>Solution :
current is a struct linked_list*, but current->head->next is a struct list_node*.
struct linked_list and struct list_node are two different unrelatd structures in spite of their similarity.
You cannot assign pointers to different types, hence the error message incompatible pointer type.
You probably want this:
void print_linked_list(struct linked_list* list) {
struct list_node* current = list->head;
while (current != NULL) {
printf("%d ", current->value);
current = current->next;
}
}