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: */