I'm trying to convert a Linked List to a Array, to this i'm creating a Array of Char* with the same size of the Linked List, then i loop through the nodes in the Linked List, allocate the new value, then i copy all the chars, and for some reason, when i run, I always gets the last bit has a random ram value, i did everything i could think to resolve this, use memcpy? Not work, change the size of the new char to sup->value->len - x? Just results in X more values to be random ram values. The only "ok" result i got is when i set lines[count][i + 1] (the last value +1) to something, and i get the value instead of something random.
char **toArray(LinkedList *lista) {
printf("toArray\n");
// String *lines = (String *)malloc(sizeof(String) * lista->len);
char **lines = malloc(sizeof(char *) * lista->len);
Node *sup = lista->start;
int count = 0;
while (sup != NULL) {
lines[count] = (char *)malloc(sizeof(char) * sup->value->len);
for (int i = 0; i < sup->value->len; i++)
lines[count][i] = sup->value->str[i];
// memcpy(lines[count], sup->value->str, sup->value->len);
printf("%s\n", lines[count]);
count += 1;
sup = sup->next;
}
return lines;
}
If you want to try here is the code, just run the cmake then ./tager Test\ Files/test1.txt
https://github.com/MarceloLuisDantas/Tager-Assembler