The problem I’m seeing is that your auxiliary pointer is always the same. To create a list, with each iteration of your for
, you should allocate a new space in memory and assign this space to your auxiliary pointer
For example:
int main()
{
// Crio o nodo "inicial" apenas para manter registro da posição inicial da lista
n *inicial = malloc(sizeof(n));
n *paux = inicial;
int qt;
printf("Quantidade de notas: ");
scanf("%d", &qt);
for (int i = 0; i < qt; i++)
{
printf("Nota 1: ");
scanf("%f", &paux->n1);
printf("Nota 2: ");
scanf("%f", &paux->n2);
paux->mf = (paux->n1 + paux->n2) / 2;
if (i + 1 == qt)
{
// Se for o ultimo item da lista, atribuo NULL para o ponteiro do próximo nodo
paux->prox = NULL;
}
else
{
// Caso contrário, aloco um novo espaço na memória para o próximo nodo, e movo o ponteiro auxiliar para essa posição
paux->prox = malloc(sizeof(n));
paux = paux->prox;
}
printf("\n");
}
// Envio o nodo com a posição inicial da lista
imprime(inicial);
return 0;
}
With the help of pointers pointing to pointers, it is also possible to get rid of the condition if
. Note that the result of this code is the same, but a code capable of treating the exception as standard is generally considered more elegant.
int main()
{
n *inicial = NULL;
n *aux;
n **paux = &inicial;
int qt;
printf("Quantidade de notas: ");
scanf("%d", &qt);
for (int i = 0; i < qt; i++)
{
(*paux) = malloc(sizeof(n));
aux = *paux;
printf("Nota 1: ");
scanf("%f", &aux->n1);
printf("Nota 2: ");
scanf("%f", &aux->n2);
aux->mf = (aux->n1 + aux->n2) / 2;
aux->prox = NULL;
paux = &aux->prox;
printf("\n");
}
imprime(inicial);
return 0;
}
Low quality question. You want to print each note?
– Maury Developer
Exactly, that in case it would be n1 and N2.
– Leh