Print on the dynamic list screen

1

Personally I can not perform the print function on the screen, could help me in this function below.

void imprime_lista(Lista* li){
 while (li != NULL)
 {
    printf("%d ", li -> struct amatricula);
    li = li -> prox;
 }
 printf("\n");
}

The structures look like this:

struct aluno {
  int matricula;
  char nome[30];
  float n1, n2, n3;
};
typedef struct elemento* Lista;

//Arquivo ListaDinEncad.c
struct elemento {
  struct aluno dadosAlunos;
  struct elemento *prox;
};
typedef struct elemento Elem;

Lista* criar_lista();

Main:

int main()
{
int varAux = 0;

struct aluno estruturaAlunoAuxiliar;
Lista *listaDeAlunos;

listaDeAlunos = criar_lista();

estruturaAlunoAuxiliar.matricula = 1;
strcpy(estruturaAlunoAuxiliar.nome, "Thiago Ferreira");
estruturaAlunoAuxiliar.n1 = 7;
estruturaAlunoAuxiliar.n2 = 6;
estruturaAlunoAuxiliar.n3 = 9;

varAux = inserir_no_inicio_da_lista (listaDeAlunos, estruturaAlunoAuxiliar);
//    varAux = imprime_lista(listaDeAlunos);
//printf("%d\n", varAux);
imprime_lista(listaDeAlunos);
return 0;
    
asked by anonymous 03.10.2017 / 16:40

1 answer

3

If dadosAluno is not a pointer then it is accessed with . .

The print 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 that it began with -> because li is a pointer. If it were not (although in these cases it is necessary for it to end in NULL ) it would be li.dadosAluno.nome .

If dadosAluno was declared as a pointer, like this:

struct elemento {
  struct aluno *dadosAlunos;
  struct elemento *prox;
};

So access to the function would look like this:

li->dadosAluno->nome

However this would imply allocating this object with malloc before using and removing it when it was not needed with free , which would considerably complicate the code.

Edit :

The error you are having:

  

request for member 'dataAlumni' in something not a structure or union

It actually has to do with a subtle error in the code, here:

void imprime_lista(Lista* /*<--aqui*/ li){

The parameter must be Lista li , not Lista* li , because Lista is already a typedef for a pointer, as you can see here:

typedef struct elemento* Lista;

Soon I was using a pointer for a pointer! As if it were struct elemento**

Edit 2 :

Just like in my last statement, Lista is already a pointer to a node in the list, so it is not supposed to use Lista* anywhere, unless it is as a parameter of a function that changes the start of the list .

This causes your main to 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 have to adjust their functions to match this new main in terms of parameters and returns.

    
03.10.2017 / 16:52