C linked list incompatible pointer type
Solution 1:
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;
}
}
Solution 2:
The function
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;
}
}
does not make a sense.
In this statement
current = current->head->next;
the pointer current
has the type struct linked_list *
while the expression current->head->next
has the type struct list_node *
.
It seems you mean
void print_linked_list( const struct linked_list *list )
{
if ( list != NULL )
{
for ( const struct list_node *current = list->head; current != NULL; current = current->next )
{
printf( "%d ", current->value );
}
}
}