Home > Blockchain >  Argument incompatible in print linked list - C
Argument incompatible in print linked list - C

Time:12-08

So I try to print a linked list, and it says that the argument head is incompatible argument type. The list is already made correctly and works if not put in another function. I just can't understand why this specific function doesn't work.

struct data {
    char foodName[FILENAME_MAX];
    int rating;
    float price;
};

typedef struct listElement {
        struct listElement *next;
        struct data food;
        struct listElement *previous;
} listElement;

void printList(listElement *head);
void printElement(listElement *element);

int main()
{
    struct data food;
    listElement head;

    printList(head); <-- this one gets an error

    return 0;
}

void printList(listElement *head)
{
    if (head == NULL) {
        printf("Linked list is empty.\n");
    } else {
        printf("Printing linked list to result file...\n");
        printElement(head);
        printf("Linked list successfully printed to result file.\n");
    }
}

void printElement(listElement *element)
{
    if (element == NULL || file == NULL) {
        return;
    } else {
        printf ("name = %s rating = %d price = %f\n", element->food.foodName, element->food.rating, element->food.price);
        printElement(element->next);
    }

CodePudding user response:

In this function void printList(listElement *head) you are expecting a pointer to a listElement but in the main function you are calling the printList function with a listElement type, not a listElement* type.

To fix this you have to declare your head variable like this listElement* head; or you have to give the printList function a pointer to the head variable, like this printList(&head);.

Either way, it depends on what you want to do with it. But the first suggestion is more common.

  •  Tags:  
  • c
  • Related