How to use pointer and vector in a single struct in C?

1

I can not run a program that needs functions to execute a procedure, this function receives a vector of type struct through a pointer. It does not have a compilation error, but the program crashes when it runs and does not work. How do I use all of this together: pointer, vector, function, and struct. Thankful.

struct dataNasc{
    int dia;
    int mes;
    int ano;
};

struct RgAluno{
    char nome[35];
    float nota[4];
    float media;
    struct dataNasc dn;
};

void ler(struct RgAluno* Aluno[]){

    int i;
    int j;

    for(i=0;i<2;i++){
        Aluno[i]->media=0;
    }

    for(i=0;i<2;i++){
    printf("nome: ");
    scanf("%s",&Aluno[i]->nome);
    printf("data de nascimento:\ndia: ");
    scanf("%d",&Aluno[i]->dn.dia);
    printf("mes: ");
    scanf("%d",&Aluno[i]->dn.mes);
    printf("ano: ");
    scanf("%d",&Aluno[i]->dn.ano);
    for(j=0;j<4;j++){
        printf("nota %d: ",j+1);
        scanf("%f",&Aluno[i]->nota[j]);
        Aluno[i]->media+=Aluno[i]->nota[j];
                    }
        Aluno[i]->media/=4;
                      }
}


int main(){

struct RgAluno* Aluno[MAX];


    ler(&Aluno);

         return 0;
}
    
asked by anonymous 05.06.2016 / 02:32

1 answer

0

Starting with your ler method, if it receives a vector, then the data type will be:

void ler(struct RgAluno Aluno[])
//ou
void ler(struct RgAluno *Aluno)

The way you declared it says that the method has to receive a vector pointer.

And after that, the declaration of your struct . Since it should only be a vector you can not use * and [MAX] , you should only use one of them.

struct RgAluno Aluno[MAX]; // cria um vetor do tamanho de MAX
//ou
struct RgAluno* Aluno = malloc(sizeof(struct RgAlune)*MAX); // cria um vetor do tamanho de MAX (Esse vetor é um ponteiro);

And finally to call your method. How to pass a vector, its variable is already a vector. there is no need to put & because you do not have to get the memory address because a vector is already a memory address. So just pass your variable normally.

ler(Aluno);

With this your variable Aluno within ler will not use -> to access addresses but . .

    
05.06.2016 / 02:42