[SOLVED] Trouble solving Invalid write of size 1 please advice
I wrote a reverse function it works fine but having trouble solving valgrind throwing
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *strrev(char *str){
char *tmp = malloc(sizeof(char) * strlen(str));
memset(tmp,0,strlen(str));
for(int i=0, k=(int)strlen(str); i<=(int)strlen(str); i++, k--){
printf("%c\n", str[k]);
strncat(tmp, &str[k], 1);
}
return tmp;
}
int main(){
char *str = strrev("ABCD");
printf("%s\n", str);
free(str);
return 0;
}
Please advice