Incompatible arguments - Pointers for structures

Asked

Viewed 276 times

3

Good afternoon,

I needed help fixing some mistakes at the terminal.

Summary:

I have done some functions to get some information (values) about active clients (where a client is active if he is an issuer or beneficiary of an unprocessed cheque). It is a system of checks and customers

I’m using structures and trees and pointers, but I have errors in the terminal. or else the functions are poorly done.

I have a main file, Item. c e h, and QUEUE. c e h if necessary I can put more parts of the code. I will put only what I think is needed.

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

ref = reference

n= number

ch= cheque

e= issued

b=beneficent

Mistakes

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);
    }
  • Don’t put the definitions of struct in the main. c. Put them in file . h and include that file in main. c and in all others . c that need these structs. Apparently the main. c has typedef struct cliente { ... } Cliente; and other files that need it (the project2.c) do not know where to find the definition.

  • I think it’s not that because I was able to compile and run the program even with the files in main, I did not put in the posts but my main and the other files have the correct includes. I think I have already solved the problem but there have been others, but soon I put my solution

  • 1

    I agree with @pmg, but I also think this is not the problem. Apparently, the signature of the command differs from the parameters passed. Please highlight lines 300 and 308 of the file main.c. Its code is very extensive, and these lines (as well as the signature of the command mostrainfocliente) are the regions related to the problem.

  • I suggest you lose this habit of using names like refe, ch, t etc. If you need a caption to know what is what, you can improve.

1 answer

1

The problem is passing parameters in the command veclientesativostree.

You’re passing a Cliente* for the call mostrainfocliente, while the signature of this command requires 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 command mostrainfocliente to receive a const Cliente* or const Cliente&. Thus, you avoid the copy of the structure Cliente, making faster u code.

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);
    }
    ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.