I'm making a trim function, i'm doing the trimLeft first, but i'm when i change the value, in the function i can print after the strcpy, but after the return, when i check the value i just get random ram values.
void trim(char *s) {
size_t original_len = strlen(s);
if (original_len == 0)
return;
if (s[0] == ' ') {
int count = 0;
for (int i = 0; i < original_len; i++) {
if (s[i] != ' ')
break;
count += 1;
}
char *trimLeft = newStr(original_len - count + 1);
for (int i = count; i < original_len; i++)
trimLeft[i - count] = s[i];
trimLeft[original_len - count] = '\0';
free(s);
s = (char *)malloc(sizeof(char) * strlen(trimLeft) + 1);
strcpy(s, trimLeft);
}
}
The full code: https://github.com/MarceloLuisDantas/Tager-Assembler
To test run cmake --build . && ./Tager Test\ Files/test1.txt