Skip to main content
6 events
when toggle format what by license comment
Mar 25, 2019 at 6:22 vote accept freshwound
Mar 25, 2019 at 6:22 comment added freshwound thank you so much for your explanation!!! :)
Mar 23, 2019 at 13:50 comment added Blauelf In the C world, a string is a pointer to a first character, and the string continues until we hit the null terminator. strcmp and other string functions rely on that one to know where to stop. Instead of char newWord[length + 1]; you could have used char *newWord = malloc((length + 1) * sizeof(char));, but the newWord[length] = '\0'; is critical, and so is having the space for this null terminator (the +1). And in your code, you forgot to free(newWord); before returning, that's the memory leak, which is trivially fixed by using an array instead of malloc.
Mar 23, 2019 at 4:05 comment added freshwound Hi! Thank you for replying. I followed your comment and managed to solve speller. However, I don't think I quite understand... do you mind elaborating more? This is my original code: int length = strlen(word); char *newWord = malloc(length * sizeof(char)); and this is what I changed it to: int length = strlen(word); char newWord[length + 1]; newWord[length] = '\0';
Mar 21, 2019 at 12:40 history edited Blauelf CC BY-SA 4.0
added 17 characters in body
Mar 21, 2019 at 12:35 history answered Blauelf CC BY-SA 4.0