How do I pass an array of structure pointers to a function?

Asked

Viewed 167 times

2

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

#define NUM_NOTAS 5
#define TAM_NOME 20


struct Aluno{
 char nome[TAM_NOME];
 int numero;
 int notas[NUM_NOTAS];
};
void preenche(struct Aluno* lista[], int tam){
 int i;
 int j;
 for(i=0;i<tam;i++){
    printf("Nome:");
    scanf("%s",&(lista[i])->nome);
    printf("Número:");
    scanf("%d",&(lista[i])->numero);
    for(j=0;j<5;j++){
        printf("Introduza a nota %d:",j);
        scanf("%d",&(lista[j])->notas[j]);
    }
 }
}



int main(int argc, char** argv) {
 struct Aluno* lista[5];
 preenche(&lista[5],5);



 return (EXIT_SUCCESS);
}

I wonder how I step struct Aluno* lista[5]; for the function preenche, because the way I’m doing the function does not read the array.inserir a descrição da imagem aqui

2 answers

2


I think what you r actually is this:

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

#define NUM_NOTAS 5
#define TAM_NOME 20

struct Aluno {
 char nome[TAM_NOME];
 int numero;
 int notas[NUM_NOTAS];
};

void preenche(struct Aluno lista[], int tam) {
    for (int i = 0; i < tam; i++) {
        printf("\nNome:");
        scanf("%s", lista[i].nome);
        printf("\nNúmero:");
        scanf("%d", &lista[i].numero);
        for (int j = 0; j < NUM_NOTAS; j++) {
            printf("\nIntroduza a nota %d:", j);
            scanf("%d", &lista[i].notas[j]);
        }
    }
}

int main(void) {
    struct Aluno lista[2];
    preenche(lista, 2);
    printf("\n----------------------");
    for (int i = 0; i < 2; i++) {
        printf("\nNome:");
        printf("%s", lista[i].nome);
        printf("\nNúmero:");
        printf("%d", lista[i].numero);
        for (int j = 0; j < NUM_NOTAS; j++) {
            printf("\nNota %d:", j);
            printf("%d", lista[i].notas[j]);
        }
    }
    return (EXIT_SUCCESS);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that you have no reason to have this pointer. And if you do, you would need to allocate memory to it. Then just pass the variable itself which is already a array (is actually a pointer after all). You can make it simple by doing it like this and you can figure out what you need.

I solved some other small problems. I had the result printed to see that it is correct.

If you want to use a pointer, then use a pointer and not the array. In this case basically the change must be in the declaration of the variable that must allocate memory otherwise. For a pointer to array must have meaning in this, which is not the case. I replied with array because that’s what’s in tag, and the code seems to indicate that it is important, while the pointer does not.

  • But my goal is to pass as a pointer, in which case what would I have to do? Because my question is to work on structures with pointers and I’d like to understand.

  • @ppfernandes But what is the reason you want to pass as a pointer? It doesn’t seem to have any, which is creating an unnecessary complication.

  • Yes, I know, but this is an exercise (from a record) that needs to be done that way, but I’ve been able to do it thanks for the help.You want me to pass my resolution to the site?

  • @ppfernandes Who asked to do the exercise is not being reasonable and is inducing you to learn wrong thing. I’m pretty sure the solution is wrong, even though it looks like it works. It works because it is a simple exercise that has a very controlled use of memory, if doing the same thing in other code will give serious errors. I keep saying that being right and functioning are very different things. To apender you must do right. This is what I showed you. In addition, the question does not contain the requirement of exercise which should have a justification for the request.

0

Just change the preenche(&lista[5],5); for preenche(lista, 5);.

Ah, I think in your scanf("%d",&(lista[j])->notas[j]); you really wanted to scanf("%d", &(lista[i])->notas[j]);. I mean, he used j instead of i.

  • When put this way, list[5] does not enter the function. Simply read list as one and not an array.

  • @ppfernandes But that’s not how I said it’s to put. It’s to put just lista, and not lista[5] and neither &lista[5].

  • Yes I used its way but that way does not pass the array.See the print above should appear list[0],list[1],.. and not only *list.

  • @ppfernandes I think your problem is in scanf. Take a look at this question: http://answall.com/q/137262/132

Browser other questions tagged

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