Incompatible Arguments - Pointers to Structures

3

Good afternoon,

I needed help fixing errors in the terminal.

Summary:

I performed some functions to get certain information (values) on active customers (where a customer is active if he or she is an issuer or beneficiary of an unprocessed check). It is a system of checks and clients

I am using structures, trees and pointers but I have errors in the terminal I think they have misused the pointers .. or the functions are badly done.

I have a main file, Item.c and h, and QUEUE.c and h if necessary I can for more parts of the code. I'll just stick to what I think it takes.

The info function prints the (summarized) information on all active system clients: 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);
    }
    
asked by anonymous 14.05.2015 / 13:02

1 answer

1

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

You are passing a Cliente* to the call mostrainfocliente , whereas the signature of this command requests 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 get a const Cliente* or const Cliente& . So you avoid copying the Cliente structure, making 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);
    }
    ...
    
14.05.2015 / 16:59