Error in passing a structure by reference C

Asked

Viewed 32 times

-2

In this exercise it is necessary to change only the characters a by b, ie output of the printf should be like this {B, 1.5, 2.7} {A, 3.9, 4.2}

typedef struct{char n;float x;float y;}Ponto;
void troca_nomes(Ponto *a, Ponto *b){
    char aux;
    aux = *a.n;
    *a.n = *b.n;
    *b.n = aux; 
}
int main(void){
    Ponto a = {'A', 1.5, 2.7};
    Ponto b = {'B', 3.9, 4.2};
    troca_nomes(&a,&b);
    printf("{%c,%.1f,%.1f}\n",a.n,a.x,a.y);
    printf("{%c,%.1f,%.1f}\n",b.n,b.x,b.y);
}
  • What do you mean, "change only the characters a by b" ? It’s not very clear what you need to do, just like what the mistake is facing.

  • the template of the printf shall be {'B', 1.5, 2.7} {'A', 3.9, 4.2}

1 answer

1

*a.n is wrong. With this you are trying to access the value of pointer position n within the structure a. But n is not a pointer, a is the pointer.

It would have to be (*a).n or a->n

Browser other questions tagged

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