I'm having write/read problems with valgrind and I don't understand why. The errors occurs on the same code block all time, changing only the memory address. The code block is:
void stringModifier(char *string) {
char *sourceString = string;
char *destinyString = sourceString;
while(*string != '\0') {
*string = tolower(*string);
if(*string != ' ') { *destinyString++ = *string; }
string++;
}
*destinyString = '\0';
}
int qsortComparison(const void *a, const void *b) {
return (*(char *)a - *(char *)b);
}
void qsortString(char *string, char *tempString) {
strcpy(tempString, string);
stringModifier(tempString);
qsort(tempString, strlen(tempString), sizeof(char), qsortComparison);
}
void outputReader(char *string1, char *string2) {
char *tempString1 = (char *) malloc (strlen(string1) * sizeof(char));
char *tempString2 = (char *) malloc (strlen(string2) * sizeof(char));
qsortString(string1, tempString1);
qsortString(string2, tempString2);
if(!strcmp(tempString1, tempString2)) { printf("V\n", string1, string2); }
else { printf("F\n"); }
}
Every time I use outputReader and call qsortString, valgrind warn a write error at strcpy and after that warn a read error at stringModifier, occurring on the same memory address.