So I have a 2d array of structs that I am storing data from a csv file. I am reading in the strings from the file then I want to assign their value in the 2d array as that string. So for example position [0][0] would be "red" and position [1][1] would be "blue" or something. The problem is that I am reading in the characters from the file and storing them in a temp string then I want to assign that temp string value to the current position I'm on. So I have the temp as "red" and I want [0][0] to be "red" then it reads more from the file and temp is now "blue" and I want [0][1] to be "blue". When I assign [0][0]=temp then [0][1]=temp they write over each other(since it is a pointer). I have tried strdup(temp) but that does not seem to fix the problem either they still re write over each other sometimes. I know I could use strcpy but for strcpy I have to have another string to copy it to so I tried that with just making a new char array each time, but that does not work either. It still gets written over. I know I could fix this problem if each time I assigned a value I created a new char array with a new name and pointed to that char array, but this is for a very large csv file so then I would have to create about 10 million char arrays and I do not know how to do that, and it seems like there should be a simpler way.
Any help is very much appreciated thanks!
struct node {
char* value;
};
char temp[500];
struct node ** arrayofnodes[35];
for(int i=0; i<35; i++) {
arrayofnodes[i] = malloc(test * sizeof (struct node));
for(int j=0; j<test; j++) arrayofnodes[i][j] = malloc(sizeof (struct node));
}
//(all of this below happens inside a function because it is going to be called like 10 million times)
arrayofnodes[row][count]->value=temp; //way 1
arrayofnodes[row][count]->value=strdup(temp); //way 2
char fun[500];
strcpy(fun,temp);
arrayofnodes[row][count]->value=fun;//way 3