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 7405391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:23:50+00:00 2026-05-29T05:23:50+00:00

I’m trying to loop through the contents of a stream to dynamically generate nodes

  • 0

I’m trying to loop through the contents of a stream to dynamically generate nodes in a list, given the contents of each line that I read. I have a struct that I defined below:

struct info {
     char *mystring;
     char *file;
     int line_no;
     struct info *next;
};

I use this loop to iterate through the stream:

while(1) {      
      char t [KMAX];
      char* file;
      char* line_no;
      char* text;
      if (fgets(t, KMAX, file_pipe) != NULL) {
           file = strtok (t, delimiter);
           line_no = strtok(NULL, delimiter);
           int line = atoi(line_no);
           text = strtok(NULL, delimiter);
           add(&head, text, line, file);
      }

I know the variables are passed correctly to my add function, because I print them out each time and I can verify it. However, the problem comes when I try to print the list. It just prints the last line of text and file name, but the integer value changes accordingly. My guess is that it has something to do with the array being destoryed and being re-created each time in the same block of memory, so the pointers get changed each time.

I’m not sure what the proper method is to go about fixing this problem. Do I modify my while loop somehow and use char pointers in a difference manner, or should I change my struct somehow to hold the variables, instead of just using pointers? I would appreciate any feedback!

EDIT : Added more code

 void add(struct info **x, char * text, int line_no, char * file) {
 struct info* current = *x;
 struct info* newInfo;


 newInfo = malloc(sizeof(struct info));
 (*newInfo).next = NULL;
 (*newInfo).grepstring = text;
 (*newInfo).line_no = line_no;
 (*newInfo).file = file;

 if (current == NULL) { //indicates the head is null, special case
      *x = newInfo;
 } else {
      //get to the end of the list
      while ((*current).next != NULL) {
           current = (*current).next;
      }
      //apends node to the end of the list
      (*current).next = newInfo;
 }

}

  • 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-05-29T05:23:51+00:00Added an answer on May 29, 2026 at 5:23 am

    When you say char* text, what you are doing is allocating a char* on the stack. strtok isn’t pointing it to some newly allocated memory, it’s pointing it into the stack-allocated space of t.

    This memory is lost when it falls out of scope, which happens at the bottom of each iteration of the while loop. Any references to its address might point to the correct contents, or nothing, or some other random value after that point – the behavior they exhibit is undefined.

    You need the contents of each struct info to survive the loop (and, presumably, continue to survive for the foreseeable future). To do this you must make a heap allocation. In C this is done via the malloc function.

    In your add method, say:

    char* text_copy = malloc(strlen(text)+1); // +1 for trailing NUL character
    strcpy(text_copy, text);
    

    This will create new memory on the heap, having the same contents as the original text.

    You should do this for all the contents of the struct info, since at the moment they’re all pointers into the stack-allocated t buffer.

    You must free the memory again when you are done with it, but it will last until then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.