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

Asked

Viewed 1,897 times

1

I cannot run a program that needs functions to execute a procedure, this function receives a vector of type struct by means of a pointer. There is no build error, but the program hangs while running and does not work. How I use all this together: pointer, vector, function and struct. Grateful.

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;
}
  • Is there a reason to make all this mess? Can’t you simplify? You can put the parts of the code that are missing?

  • There are missing parts friend, I solved as for the program to work, now I would like to know how to constitute a "pointer vector". Grateful

1 answer

0


Starting with your method ler, 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 stated it says the method has d receive a vector pointer.


And after that, the statement of his struct. Since it should only be a vector, one cannot use the * and the [MAX], should use only 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. As you pass a vector, its variable is already a vector. no need to put the & because you don’t have to take the memory address, because an array is already a memory address. So just pass your variable normally.

ler(Aluno);

With that your variable Aluno inside ler will no longer use -> to access the addresses and yes ..

  • The exercise statement is "use a pointer vector", so I had to declare struct as struct Rgaluno * Student[MAX];

  • @Raphaelzanarelli The pointer vector you say, is a vector made with pointers or a pointer that stores vectors?

  • A pointer vector, can help?

  • @Raphaelzanarelli, I believe that you do not know whether to express or I am not understand, from what I have analyzed in your code, you should use a vector or a pointer, if you use a pointer vector, your code will not run and will give memory errors. the tests I did with your code, they worked correctly doing what I described in the reply and exchanging -> for .

  • I also have this question of interpretation regarding the statement that asks for a pointer vector, however, here also rotated as you recommended. Then I clarify about the pointer vector with my teacher. Thank you for your attention.

  • @Raphaelzanarelli pointer vector or vector pointer, usually used to create a vector of strings with different sizes, such as an array, the pointer and the vector work the same way in C, the difference being that the pointer vc needs to allocate, dislocate and can change the size, already the vector has a fixed size.

Show 1 more comment

Browser other questions tagged

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