kostek
kostek

Reputation: 801

error: incompatible type for argument

I'm writing a list in C. Below is the source:

#include <stdio.h>
#include <stdlib.h>


struct list {
 int value;
 struct list *next;
};

typedef struct list ls;



void add (ls **head, ls **tail, int val)
{
 ls *new, *tmp1, *tmp2;

 if (NULL == *head)
 {
    new = (ls*)malloc(sizeof(ls));
    *head = new;
    *tail = new;
    new->value = val;
    new->next = NULL;

    return;
 }

 else
 {
    tmp1 = *head;
    tmp2 = tmp1->next;
    while (tmp2 != NULL)
    {
        tmp1 = tmp2;
        tmp2 = tmp1->next;
    }

    new = (ls*)malloc(sizeof(ls));
    new->value = val;
    new->next = NULL;
    *tail = new;

    return;
 }
}



void show (ls **head, ls **tail)
{
 int i;
 ls *tmp;

 while (tmp->next != NULL)
 {
    printf("%d: %d", i,  tmp->value);
    i++;
    tmp=tmp->next;
 }

 return;
}



int main (int argc, char *argv[])
{
 ls *head;
 ls *tail;
 int n, x;

 head = (ls*)NULL;
 tail = (ls*)NULL;

 printf("\n1. add\n2. show\n3. exit\n");
 scanf("%d", &x);
 switch (x)
 {
    case 1:
        scanf("%d", &n);
        add(*head, *tail, n);
        break;

    case 2:
        show(*head, *tail);
        break;

    case 3:
        return 0;

    default:
        break;
}

 return 0;
}

When I compile it with gcc

gcc -o lab5.out -Wall -pedantic lab5.c

I get strange errors:

lab5.c: In function ‘main’:
lab5.c:84:3: error: incompatible type for argument 1 of ‘add’
lab5.c:16:6: note: expected ‘struct ls **’ but argument is of type ‘ls’
lab5.c:84:3: error: incompatible type for argument 2 of ‘add’
lab5.c:16:6: note: expected ‘struct ls **’ but argument is of type ‘ls’
lab5.c:88:3: error: incompatible type for argument 1 of ‘show’
lab5.c:52:6: note: expected ‘struct ls **’ but argument is of type ‘ls’
lab5.c:88:3: error: incompatible type for argument 2 of ‘show’
lab5.c:52:6: note: expected ‘struct ls **’ but argument is of type ‘ls’

For me everything is OK...

And argument type is ls** and not ls as compiler says.

Someone see what can be wrong?

PS. I know that it's unnecessary to give *tail as argument and it's unused, however it will be, because I want to develop this 'program'...

Upvotes: 3

Views: 51160

Answers (2)

Scott M
Scott M

Reputation: 395

As Daniel said in his comment and codaddict said in his answer, using & instead of * will give you what you want. Here's a bit of an explanation though, to help you remember.

* de-references what it's attached to, meaning it takes the variable as if it's a pointer and gives the value at that address.

& passes the reference, meaning it gives the address that the value is stored at, or rather, passes a pointer to the variable.

Since you want to pass a pointer to the variables head and tail, you want to pass them as &head and &tail, which gives you the values **head and **tail at the other end. It seems counter-intuitive until you get used to it.

Upvotes: 4

codaddict
codaddict

Reputation: 455312

add(*head, *tail, n);

should be :

add(&head, &tail, n);

Since you need to pass the address of the head and tail pointers to the function.

Similar fix needs to be made to the call to the show function.

Upvotes: 1

Related Questions