Error accessing a struct

Asked

Viewed 35 times

0

I created a function to read how many hours the employee worked in the week, however, I have problems effectively saving in the memory of struct.

Calling the function:

lerHoraExtra(&funcionario[i].horasExtra);

The function itself only receives the struct employee and in for loop, user must add how many hours worked in the week.

float lerHoraExtra(struct funcionario *func){
  int i,j;
  printf("Considere que a semana comece na segunda-feira(dia 1)\n");

    for(j=0; j<SEMANA; j++){
      printf("\nInforme quantas horas extra o funcionario fez no dia %d: ", j+1);
      scanf("%f", &func[i].horasExtra[j]);
  }
}

However, when performing the calculation and saved in the file, it is not saved, I am sure the error is in the function, but I do not know how else could do.

To struct:

typedef struct funcionario{
  int id;
  char nome[101];
  float horasTrab;
  float salarioHora;
  float horasExtra[SEMANA];
  float valorExtra;
}funcionario[TAM];
  • 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.

  • 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

  • 1

    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.

1 answer

1


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.

Browser other questions tagged

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