I am getting segmentation fault
in the below while
loop.
int main() {
register char *str = "Hello World";
char *ans;
ans = test(str);
if(!ans)
return 1;
free(ans);
return 0;
}
char *test(char *str) {
char *str1 ;
char *str2;
char *str3;
str1 = malloc(strlen(str) + 5);
str2 = str;
str3 = str1;
*str3++ = '\b';
*str3++ = '\b';
while(*str2)
*str3++ = *str2++;
*str3++ = '\f';
*str3++ = '\f';
*str3 = '\0';
return (str1);
}
I think I am getting segmentation fault
in while
loop. Could you please suggest why? I am calling this as char *ans = test(string)
where string
is register char *string
. Let us say, I have hello world
in the string. My intention is to return \b\bHello World\f\f
from test()
.