Incompatible arguments – Pointers to structures

Question:

Good afternoon,

I needed help fixing some errors in the terminal.

Summary:

I did some functions to obtain certain information (values) about active customers (where a customer is active if they are the issuer or beneficiary of an unprocessed check). It's a check and customer system

I'm using structures, trees and pointers but I have errors in the terminal I think they are from having misused the pointers… or else the functions are wrongly done.

I have a main file, Item.ceh, and QUEUE.ceh if necessary I can put more parts of the code. I'll just put in what I think is necessary.

The info function prints (summarized) information about all active clients in the system: ref nche vche nchb vchb

ref = reference

n= number

ch=check

e= issued

b=beneficiary

errors

miguel@maa10Linux:~/iaed/proj2/codigo/versaofinal$ gcc -Wall -ansi -pedantic -o proj2 proj2.c Item.c QUEUE.c
proj2.c: In function ‘veclientesativostree’:
proj2.c:308:4: error: incompatible type for argument 1 of ‘mostrainfocliente’
    mostrainfocliente(c);
    ^
proj2.c:300:6: note: expected ‘Cliente’ but argument is of type ‘struct Cliente *’
 void mostrainfocliente(Cliente c){
  ^

files

Item.c

[…]

typedef struct  {                   
    int valor;
    long int refe;
    long int refb;
    long int refc;


} Item; 

[… ]

QUEUE.c

[…]

void QUEUEput(Item item) {
    if (head == NULL) {
        head = tail = NEW(item, head);
    }
    else {
        tail->next = NEW(item, tail->next);
        tail = tail->next;
    }
}




Item QUEUEget() {         
    Item item = head->item;
    queuelink t = head->next;
    free(head);
    head = t;
    return item;
} 

Item QUEUEfind(long refc, int (*cmp)(Item,long))
{
    queuelink t;
    Item item;
    item.refc = -1;

    for(t = head; t != NULL; t = t->next)
        if(cmp(t->item, refc) == 0)
            return t->item;
    return item;
} 

Item QUEUEdelete(long refc, int (*cmp)(Item,long))
{
    queuelink t, prev;
    Item item;
    item.refc = -1;

    for(t = head, prev = NULL; t != NULL;
        prev = t, t = t->next) {
        if(cmp(t->item, refc) == 0) {
            if(t == head)
                head = t->next;
            else
                prev->next = t->next;
            if (t == tail) tail = prev;
            item = t->item;              /* fazer funcao */
            free(t);
            return item;
        }
    }
    return item;
}

Main.c

[…]

typedef struct cliente {
    long ref, nche, vche, nchb, vchb;
} Cliente;


typedef struct node {
    Cliente *item;
    struct node *l;
    struct node *r;
} *Link;

[…]

extern queuelink head, tail;
Link root;

int main()
{   

    char* comando;
    comando = (char*)malloc(sizeof(char*)*MAXcomando);

    QUEUEinit();

    do { scanf("%s", comando);

        if(strcmp(comando, "cheque") == 0){ 
            comando_cheque();
    }
        else if (strcmp(comando, "processa") == 0){
            comando_processa(); 
    }
        else if (strcmp(comando, "processaR") == 0){
            comando_processaR();    
    }
        else if (strcmp(comando, "infocheque") == 0){
            comando_infocheque();   
    }
        else if (strcmp(comando, "infocliente") == 0){
            comando_infocliente();  
    }
        else if (strcmp(comando, "info") == 0){
            comando_info();
    }
    }while (strcmp(comando, "sair") != 0);
        sair();



    return 0;
}




/* -----------------------------------FUNCOES------------------------------------*/


/* --------------- Clientes-arvore ------------------ */

Link insere(Link t, long ref, long valor)
{
    if (t == NULL) {
        t = (Link) malloc(sizeof(struct node)); 
        t->item = (Cliente*)malloc(sizeof(Cliente));
        t->item->ref = ref;
        t->item->nche = t->item->vche = t->item->nchb = t->item->vchb = 0;
        t->l = t->r = NULL;     
    } else if (t->item->ref > ref) { t->l = insere(t->l, ref, valor); 
        return t; }
      else if (t->item->ref < ref) { t->r = insere(t->r, ref, valor); 
        return t; }

    if (valor > 0) {
        t->item->nche++;
        t->item->vche += valor;
    } else {
        t->item->nchb++;
        t->item->vchb -= valor;
    }
    return t;
}

Cliente *TREEget(Link t, long ref){
    if (t == NULL) 
        return NULL;
    else if (t->item->ref > ref) 
        return TREEget(t->l, ref);
    else if (t->item->ref < ref)
        return TREEget(t->r, ref);
    return t->item;
}

void TREEprint(Link t){
    if (t == NULL) 
        return;
    TREEprint(t->l);
    if (t->item->nche != 0 || t->item->nchb != 0)
        printf("*%ld %ld %ld %ld %ld\n", t->item->ref, t->item->nche, t->item->vche, t->item->nchb, t->item->vchb);
    TREEprint(t->r);
}

void TREEinit(){
    root = NULL;
}


void print(Link t)
{
    if (t == NULL) 
        return;
    print(t->l);
    printf("%ld", t->item->ref);
    print(t->r);
}



/* -------------------------------------Comandos--------------------------------------------*/

/* comando Cheque */

Item cheques;

void imprimeCheque(Item cheques) {  
    printf("Valor:%d Emissor:%ld  Cheque nº:%ld Recetor:%ld\n", cheques.valor, cheques.refe, cheques.refc, cheques.refb);
}


void comando_cheque(){

    Item newcheque;
    scanf(" %d %ld %ld %ld", &newcheque.valor, &newcheque.refe, &newcheque.refb, &newcheque.refc);
    QUEUEput(newcheque);
    imprimeCheque(newcheque);
    /* actualiza clientes */
    root = insere(root, newcheque.refe, newcheque.valor);  
    root = insere(root, newcheque.refb, -newcheque.valor);
}


/* processa */

void comando_processa() {
    Item item;
    Cliente *c;

    if (head == NULL)
        printf("Nothing to process\n");
    else {
        item = QUEUEget();

        /* actualizar clientes quando se tira da pool o cheque ha mais tempo emitido */
        c = TREEget(root, item.refe);       
        c->nche--;
        c->vche -= item.valor;
        c = TREEget(root, item.refb);
        c->nchb--;
        c->vchb -= item.valor;

    }
}


/* processaR */

int compare(Item it, long ref) { 
    return it.refc - ref; }

void comando_processaR() {
    long int ref;
    Item item;
    Cliente *c;

    scanf("%ld", &ref);
    item = QUEUEdelete(ref, compare);
    /* actualizar info */
        c = TREEget(root, item.refe);
        c->nche--;
        c->vche -= item.valor;
        c = TREEget(root, item.refb);
        c->nchb--;
        c->vchb -= item.valor;

}





/* infocheque */
void comando_infocheque(){
    long int ref;
    Item item;
    scanf("%ld", &ref);
    item = QUEUEfind(ref, compare);
    if (item.refc == -1) 
        printf("Cheque %ld does not exist\n", ref);
    else 
        printf("Cheque-info: %ld %d %ld --> %ld\n", item.refc, item.valor, item.refe, item.refb);

}


/* infocliente */
void comando_infocliente(){
    Cliente *c;
    long int ref;
    scanf("%ld", &ref);
    c = TREEget(root, ref);
    if (c == NULL || (c->nche == 0 && c->nchb == 0)) 
        printf("Cliente %ld does not exist\n", ref);
    else 
        printf("Cliente-info: %ld %ld %ld %ld %ld\n", c->ref, c->nche, c->vche, c->nchb, c->vchb);




}


/* info */

int max(int a, int b)
{
    return (a > b)? a : b;
} 
int clienteativo(Cliente *c){

    return ( (c->nche != 0) ||  (c->nchb != 0) );
} 
int clienteativototal(Link t){

    if (t == NULL){
        return 0; }
    else  {
        if (clienteativo(t->item)){
            return clienteativototal(t->r)+ clienteativototal(t->l) + 1;}
        else {
            return clienteativototal(t-> r) + clienteativototal(t-> l);}
    }
}

void comando_info(){
    if (clienteativototal(root) == 0) 
        printf("No active clients\n");
    else 
        veclientesativostree(root);
}

void mostrainfocliente(Cliente c){
    printf("* %ld %ld %ld %ld %ld\n", c.ref, c.nche, c.vche, c.nchb, c.vchb);
}
void veclientesativostree (Link t){
    Cliente *c;
    if (t!=NULL)
        veclientesativostree(t-> l);
        if (clienteativo(c)){
            mostrainfocliente(c);
        }
    veclientesativostree(t-> r);
    }

Answer:

The problem is the passing of parameters in the command veclientesativostree .

You are passing a Cliente* to the mostrainfocliente call, while signing this command asks for a Cliente .

Change to:

void veclientesativostree (Link t){
    Cliente *c;
    if (t!=NULL)
        veclientesativostree(t-> l);
        if (clienteativo(c)){
            mostrainfocliente(*c); // necessário fazer a de-referência.
        }
    ...

Another option (better, in my opinion), is to change the mostrainfocliente command to receive a const Cliente* or const Cliente& . This way, you avoid copying the Cliente structure, making your code faster.

Anyway, the result would be:

void mostrainfocliente(const Cliente &c){
    printf("* %ld %ld %ld %ld %ld\n", c.ref, c.nche, c.vche, c.nchb, c.vchb);
}

void veclientesativostree (Link t){
    Cliente *c;
    if (t!=NULL)
        veclientesativostree(t-> l);
        if (clienteativo(c)){
            mostrainfocliente(*c);
        }
    veclientesativostree(t-> r);
    }
    ...
Scroll to Top