Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C linked list incompatible pointer type

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading