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

  • Home
  • SEARCH
  • 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 3330652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:30:42+00:00 2026-05-17T23:30:42+00:00

I’m having trouble catching what I’m doing wrong inserting strings into an array in

  • 0

I’m having trouble catching what I’m doing wrong inserting strings into an array in C. Below is the code. The printf lines show me that I am indeed reading the correct data — but after I stick each line of data into an array — I then inspect it to find that every element of the array is filled with the last line put into the array.

//Code snippet

char outbuf[BUFFER_LEN];
int std_out_count = 0;
char *std_out_lines[BUFFER_LEN]; /* Array to hold STDOUT */

while ((i = read(stdout_pipe_fds[0], outbuf, BUFFER_LEN - 1)) > 0) {
  outbuf[i] = '\0';
  char temp[BUFFER_LEN];
  printf("PARENT read from stdout: %s\n", outbuf);
  strcpy(temp, outbuf);
  std_out_lines[std_out_count] = temp;
  std_out_count++;
}

printf("STDOUT_COUNT: %i\n", std_out_count); /* Print out elements of array to make sure they're correct */
int idx;
for(idx = 0; idx < std_out_count; idx++) {
  printf("STDOUT[%i]: %s\n", idx, std_out_lines[idx]);
}

// I can see by this output that I’m reading the correct values

/*
PARENT read from stdout: STDOUT = :this is line number 0:

PARENT read from stdout: STDOUT = :this is line number 1:

PARENT read from stdout: STDOUT = :this is line number 2: */

// But when I inspect the elements of the array — every element is filled with the last line read ;(

/*
STDOUT_COUNT: 3
STDOUT[0]: STDOUT = :this is line number 2:

STDOUT[1]: STDOUT = :this is line number 2:

STDOUT[2]: STDOUT = :this is line number 2: */

  • 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-17T23:30:42+00:00Added an answer on May 17, 2026 at 11:30 pm

    This line:

    char *std_out_lines[BUFFER_LEN];
    

    allocates an array of character pointers but does not allocate any space for the actual data itself.

    Later on, when you do:

    char temp[BUFFER_LEN];
    printf("PARENT read from stdout: %s\n", outbuf);
    strcpy(temp, outbuf);
    std_out_lines[std_out_count] = temp;
    

    you’re actually overwriting the same piece of memory which each new line and storing its (unchanging) address into the next character pointer.char temp[BUFFER_LEN]; doesn’t give you a new data area every time it executes.

    That’s why it appears to be working. However, when temp goes out of scope, I wouldn’t suggest trying to examine any of the lines pointed at by your array. It’s undefined behaviour. It seems to be working for you but I’ll guarantee that’s purely by accident 🙂

    What you may want to look at is duplicating the string so that each array element has its own copy. Replace:

    std_out_lines[std_out_count] = temp;
    

    with:

    std_out_lines[std_out_count] = strdup(temp);
    

    or, just ditch temp altogether and use:

    std_out_lines[std_out_count] = strdup(outbuf);
    

    This makes a copy of the string, and gives you the address of it for storing into the array, so that they’re all separate from each other. Just remember that you should probably free that memory eventually.


    On the off-chance that your C implementation doesn’t have a strdup (it’s not mandated by the ISO standard), here’s one I prepared earlier.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.