Struct with vector is not working

1

I'm trying to use vectors and struct , but it's not working.

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

typedef struct Atleta {

    float notas[5];

} atleta;

void receberNotas(atleta* l) {
    int i;
    for(i=0; i<5; i++) {
        printf("Digite %d nota: ", (i+1));
        scanf("%f", &l[i].notas);
    }
}

void mostrarNotas(atleta *l) {
    int i;
    for(i=0; i<5; i++) {
        printf("\n%.2f", l[i].notas);
    }
}
int main()
{
    atleta *a;
    receberNotas(&a);
    mostrarNotas(a);
    return 0;
}

I could not use this operator - > to access.

    
asked by anonymous 28.09.2016 / 00:59

1 answer

2

There are several problems. I'm assuming that an athlete has 5 grades. If it is not, there are more things wrong.

You have to allocate memory for the structure. I preferred to opt for dynamic allocation with malloc() . And do not need to pass & , it is already a pointer .

Access to the index must be done in notas and not in the athlete object, in the case l (bad variable name) was used. Unless there are 5 athletes with one note each (it would be weird to use a framework for this).

And member access must be done with the -> operator already which is a reference.

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

typedef struct Atleta {
    float notas[5];
} atleta;
void receberNotas(atleta* l) {
    for(int i = 0; i < 5; i++) {
        printf("Digite %d nota: ", (i + 1));
        scanf("%f", &l->notas[i]);
    }
}
void mostrarNotas(atleta *l) {
    for(int i = 0; i < 5; i++) {
        printf("\n%.2f", l->notas[i]);
    }
}
int main() {
    atleta *a = malloc(sizeof(atleta));
    receberNotas(a);
    mostrarNotas(a);
}

See running on ideone .

See the Actual difference between operator point (.) and operator arrow (->) in C?

    
28.09.2016 / 01:14