Rafał Duraj
Rafał Duraj

Reputation: 1

C cannot add new element to struct

I have such a structure:

typedef struct kodProgramu {
    char* etykieta;
    char* instrukcja;
    char* operand;
    struct kodProgramu *nast;
} kodPrg;

This code is for adding new element:

void pobierzKodStdin(kodPrg *kod, char *wynik, char *linia, int flagaEtyk)
{
    wynik = fgets(linia, 80, stdin);
    while(wynik != NULL)
    {
        kodPrg *wsk, *nowy;
        wsk = kod;
        while(wsk->nast != NULL)
            wsk = wsk->nast;

        if(linia[0] == ' ')
            flagaEtyk = 1;

        nowy = (kodPrg*)malloc(sizeof(kodPrg));
        int licznik = 0;
        char *pch;
        pch = strtok(linia, ":# ");
        while(pch != NULL)
        {
            if(flagaEtyk == 0)
            {
                if(licznik == 0)
                    nowy->etykieta = pch;
                else if(licznik == 1)
                    nowy->instrukcja = pch;
                else if(licznik == 2)
                    nowy->operand = pch;
            }
            if(flagaEtyk == 1)
            {
                if(licznik == 0)
                    nowy->instrukcja = pch;
                else if(licznik == 1)
                    nowy->operand = pch;
            }

            licznik++;
            pch = strtok(NULL, ":# ");
        }
        nowy->nast = NULL;
        wsk->nast = nowy;

        flagaEtyk = 0;
        wynik = fgets(linia, 80, stdin);
    }
}

This function print this structure to the console:

void wypiszKod(kodPrg *kod)
{
    kodPrg *wsk = kod;
    while(wsk != NULL)
    {
        printf("%s %s %s\n", wsk->etykieta, wsk->instrukcja, wsk->operand);
        wsk = wsk->nast;
    }
}

This is my main function:

int main()
{
    char linia[80], *wynik;
    char *wsk = malloc(sizeof(char));
    int flagaEtyk = 0;
    //tasmaWejsc *wejscie = (tasmaWejsc*)malloc(sizeof(tasmaWejsc));
    //tasmaWyjsc *wyjscie = (tasmaWyjsc*)malloc(sizeof(tasmaWyjsc));
    //wyjscie->wartosc = 0;
    //wyjscie->nast = NULL;
    kodPrg *kod = (kodPrg*)malloc(sizeof(kodPrg));
    kod->etykieta = " ";
    kod->instrukcja = " ";
    kod->operand = " ";
    kod->nast = NULL;
    int liczba;

    //wprowadzWejscie(wynik, linia, wejscie);
    //wypiszWejscie(wejscie);

    //system("cls");

    pobierzKodStdin(kod, wynik, linia, flagaEtyk);
    wypiszKod(kod);

    return 0;
}

Now, when I enter one line like : test test test It's working good and print test test test in console.

But when I enter more lines, for example:

test test test
xxxx xxxx xxxx

The program is printing:

xxxx xxxx xxxx
xxxx xxxx xxxx

It's like the second line replace the first one. I don't know why, when I have a struct with int instead of char * it's working good. Next element are added and it' printing good, but when char * it's working as I described above.

How to add new elemnt to the list when I have struct with char *?

Upvotes: 0

Views: 943

Answers (2)

mouviciel
mouviciel

Reputation: 67889

Every string in your linked list is mapped to portions of linia[80], which is overwritten at each line access.

Some strdup() calls should solve your issue.

Upvotes: 0

AndersK
AndersK

Reputation: 36092

I think you should realize that strtok works on a static buffer

So when you write like this

pch = strtok(linia, ":# ");
while(pch != NULL)
{
  if(flagaEtyk == 0)
  {
     if(licznik == 0)
     nowy->etykieta = pch;

you are assigning the pointer in your shiny heap element to a string that will disappear by the next line (linia).

wynik = fgets(linia, 80, stdin);

what you need to do is to make a copy of the string, this can be done using strdup()

     nowy->etykieta = strdup(pch);

Upvotes: 0

Related Questions