I'm writing this function to return a char pointer of reversed string.
void * PreverseStr (char str[])
{
int size = strlen (str);
char *returnstr = (char *)malloc (size * sizeof(char));
for (int i = size - 1; i >= 0 ; i--)
{
*returnstr = str[i];
returnstr ++;
}
returnstr = 0;
returnstr -= size;
return returnstr ;
}
To test this function I wrote a main function like this
int main()
{
char str[] = "abcdefghijklmnopqrstuvwxyz";
char *newstr = PreverseStr(str);
printf("Reversed string using pointer: %s\n", newstr);
free(newstr);
return 0;
}
But it crashes before it could print out anything. I wonder what's wrong with my code. It would be much helpful if you can explain a fix to this.