Error in struct member access passed by reference

0

I have the code below that has a function to set the salary = 4000, but it returns the following error:

  

[Error] request for member 'salary' in something not a structure or union

The error occurs on the line that I try to set the salary. I'm using parameter passing by reference, since parameter passing by value does not change the salary of Joao , because the changes only occur within the function, not being reflected in the salary of it.

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

typedef struct{
    int idade;
    int salario;
}PESSOA;

void setSalario(PESSOA *p){
    *p.salario = 4000;
}

int main(int argc, char *argv[]) {

    PESSOA joao;

    setSalario(&joao);

    printf("Salario do joao: %d",joao.salario);
}
    
asked by anonymous 22.06.2014 / 17:07

2 answers

4

It's a matter of operator precedence. The . operator has higher precedence over * unary. Then:

*p.salario

It's read as:

*(p.salario)

And since p is not a struct, it is a pointer to one, you can not write p.salario .

You can correct using parentheses:

(*p).salario

Or rather, use -> to read fields from a pointer to struct:

p->salario
    
22.06.2014 / 17:12
0

I found the problem.

In the line that defines the salary:

 *p.salario = 4000; // errado
(*p).salario = 4000; // correto
    
22.06.2014 / 17:11