Question

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

typedef struct lista
{
    char instrumento;
    char *nota;
    int inst;
    struct lista *prox;
}melodia;/*com typdef basta usar t_melodia não é preciso usar struct melodia*/



melodia *inicio, *fim; ///dois apontadores para definir o inico e o fim da lista

void inserir_inicio(char *v)
{
    if(inicio==NULL)
    {
        inicio = malloc(sizeof(melodia));
        inicio->nota = v;///nota fica com o valor recebido
        inicio->prox = NULL;///colocamos o prox para NULL
        fim = inicio;
    }
    else
    {
        melodia *temp = malloc(sizeof(melodia));
        temp->nota = v;
        temp->prox = inicio;
        inicio = temp; /// Colocamos o novo elemento no inicio
    }
    printf("Inserido no inicio\n");
}


void imprimir_lista()
{
    melodia *aux = inicio;
    printf("Lista:\n");
    while(aux!=NULL)
    {
        printf("%s", aux->nota);
        aux = aux->prox; /// proximo
    }
    printf("\n\n");
}

int main()
{
    int i = 0;
    char n[100];
    while(i<5)
    {
    scanf("%s",n);
    inserir_inicio(n);
    i++;
    }
    imprimir_lista();
}

I think the problem is in my function imprimir_lista, i just can´t get it to print to the console the values i insert in the list....(the code is in portuguese if you are asking yourselves, and sorry for my english)

Était-ce utile?

La solution

You are always passing the same address to inserir_inicio() (the n).

Every time you change the contents of n all the nota pointers in the list all point to the new contents.

You may need to copy the input to brand new objects.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top