If the dadosAluno
is not a pointer so is accessed with .
.
The printing function should then be:
void imprime_lista(Lista* li){
while (li != NULL)
{
printf("Nome: %s ", li->dadosAluno.nome /*<--diferente aqui*/);
li = li -> prox;
}
printf("\n");
}
Notice it started with ->
because li
is a pointer. If it were not(although in these cases it is necessary that it be so that it can end in NULL
) would be li.dadosAluno.nome
.
If the dadosAluno
had been declared as a pointer, thus:
struct elemento {
struct aluno *dadosAlunos;
struct elemento *prox;
};
Then the access in the function would already be like this:
li->dadosAluno->nome
However this would imply allocating this object with malloc
before using and removing when not needed with the free
, which would considerably complicate the code.
Edit:
The mistake you’re making:
request for Member 'dataAlunos' in Something not a Structure or Union
It’s actually about a subtle error in the code here:
void imprime_lista(Lista* /*<--aqui*/ li){
The parameter shall be Lista li
, and not Lista* li
, for Lista
is already a typedef
for a pointer, as you can see here:
typedef struct elemento* Lista;
Soon I was using a pointer to a pointer! As if it were struct elemento**
Edit 2:
Just like my last appointment, Lista
is already a pointer to a list node, so you’re not supposed to use Lista*
nowhere, unless it is as a parameter of a function that changes the start of the list.
This makes yours main
should be:
int main()
{
int varAux = 0;
struct aluno estruturaAlunoAuxiliar;
Lista listaDeAlunos; //sem Lista* agora
listaDeAlunos = criar_lista(); //este agora deve devolver um Lista e não Lista*
estruturaAlunoAuxiliar.matricula = 1;
strcpy(estruturaAlunoAuxiliar.nome, "Thiago Ferreira");
estruturaAlunoAuxiliar.n1 = 7;
estruturaAlunoAuxiliar.n2 = 6;
estruturaAlunoAuxiliar.n3 = 9;
//deve rever esta função pois ela para estar bem implementada deveria receber um
//Lista* para poder modificar o inicio da lista, o que faz com que deva ser passado o
//endereço da lista aqui no main
varAux = inserir_no_inicio_da_lista (&listaDeAlunos, estruturaAlunoAuxiliar);
imprime_lista(listaDeAlunos); //imprime agora recebe Lista e não Lista*
return 0;
}
You will now need to adjust the respective functions to match this new main
in terms of parameters and returns.
I imagine there’s some error in the structures as well, but it’s been a long time since I’ve made a list in C and I won’t remember. Only, to print something on
imprime_lista
, I imagine, you should changeli -> struct amatricula
forli->dadosAluno.matricula
– Felipe Avelar