Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:38:59+00:00 2026-06-17T14:38:59+00:00

I have such a structure: typedef struct kodProgramu { char* etykieta; char* instrukcja; char*

  • 0

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 *?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:39:01+00:00Added an answer on June 17, 2026 at 2:39 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a structure such as typedef struct _people { char *name; bool
Assuming I have a structure such as: typedef struct { char * string1; char
I have such a C++ structure: typedef struct _FILE_OP_BLOCK { unsigned short fid; //
I have a structure such as typedef struct FT_Bitmap_ { int rows; int width;
I have a C structure that looks like this typedef struct event_queue{ Event* event;
I have this structure which I want to write to a file: typedef struct
I have a data structure something like this: typedef struct tagSUB_DATA { double measuredValue;
Suppose I have code that maintains a parent/children structure. In such a structure I
Suppose I have a structure in C or C++, such as: struct ConfigurableElement {
I have such code with 2 structures: #include <list> using namespace std; struct Connection;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.