What I want to do is take the string, save it into an array of strings and then modify each copy based on index.
I edited the question and code, it was messy and unclear. As the question is almost the same (I think now it's more precise), I thought I could edit it entirely here without creating a new question, but let me know if I have to do differently.
PROBLEM (EDIT): after reading the answer given, creating an MVCE, and reading this and some tips to debug, I think I am doing a mess with pointers and strcpy... Why does the following code (edited to be MVCE) gives this output?
abc
x
x
y
It compiles and gives no debug errors, but I want the code to change the first char of the string in line_ret to "x" if index==0, and to "y" if index==1. I read here it's not possible to change a single char in what a pointer points to, but what if I don't know how many times I have to copy line_read into line_ret, thus don't know the maximum index size to declare the array line_ret?
Code (EDIT):
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
size_t len = 10;
int main(void){
char *line_read = malloc(5);
strcpy(line_read, "abc");
char **line_ret = malloc(5 * sizeof(char*));
int index = 0;
while(index < 2){
line_ret[index] = realloc(line_ret, 1*len);
memcpy(&line_ret[index], &line_read, len);
printf("%s\n", line_ret[index]);
if(index == 0){
strcpy(&line_ret[index][0], "x");
} else if(index == 1){
strcpy(&line_ret[index][0], "y");
}
printf("%s\n", line_ret[index]);
index++;
}
free(line_read);
free(line_ret);
return 0;
}