The code has some problems and I won’t talk about all of them, but that would be it:
#include <stdio.h>
#define SEMANA 7
#define TAM 1
typedef struct {
int id;
char nome[101];
float horasTrab;
float salarioHora;
float horasExtra[SEMANA];
float valorExtra;
} Funcionario;
void lerHoraExtra(Funcionario *funcionario) {
printf("Considere que a semana comece na segunda-feira(dia 1)\n");
for (int i = 0; i < SEMANA; i++) {
printf("\nInforme quantas horas extra o funcionario fez no dia %d: ", i + 1);
scanf("%f", &funcionario->horasExtra[i]);
}
}
int main(void) {
Funcionario funcionarios[TAM];
lerHoraExtra(&funcionarios[0]);
for (int i = 0; i < SEMANA; i++) printf("\n%f", funcionarios[0].horasExtra[i]);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I created the variable with local scope that is much better than creating global. I used the defined type, it makes no sense to create it and not use it. This variable has a plural name because it is a collection of data and not a single data. It may sound silly but these things help give the right understanding.
I didn’t make a code to be very correct, just to function and demonstrate how it is.
I just passed the element that I want the reading to be done. That’s not the best way to do it, but I followed what I started. Of course in real code could be in a loop and pass through the variable i
can make sense.
It is good to reinforce the idea that is passing by reference only one element and not the whole collection.
Then I send to read the values and save the field of extra hours of the element. So I must access every extra hour as an element of a collection, but the general object I’m going to access cannot be like a array because he’s not a array.
And I need to use the right operator when access is by reference and not by value, so I used the ->
.
You need to understand all the mechanisms you’re using before you use them. I made a minimal, complete and verifiable example, which is ideal to demonstrate something to other people.
That one
i
that appeared out of nowhere should be the problem. You passed an element and then access as array, doesn’t make sense.– Maniero
If I remove [i] from the function, error appears
[cquery] member reference type 'struct funcionario *' is a pointer; did you mean to use '->'?
and even applying ->, I still have problems– Gustavo Scholze
When you remove something that you had randomly placed in the code the compiler tells you exactly what the bug is and how to fix it. Use compiler to your advantage.
– Maniero