Could someone explain what is the most correct usage of free() function in this paricular case:
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverseStr(const char **str)
{
int i, j;
int word_len = strlen(*str);
char *tmp_word = (char *)malloc(word_len + 1);
if(tmp_word == NULL) return;
for(i=0, j=(word_len - 1); j>=0; i++, j--)
{
tmp_word[i] = (*str)[j];
}
tmp_word[word_len] = '\0';
*str = tmp_word;
// Putting free(tmp_word) here seems to erase the content of my_str
// in main() function
return;
}
int main(void)
{
const char *my_str = "This is nice sentence.";
printf("%s\n", my_str);
reverseStr(&my_str);
printf("%s\n", my_str); // now the string is reversed
return 0;
}