So I'm trying to reverse a string, but I get a memory fault. Memory for s and s1 is initialized enough to accomodate the '/0' character as well.
Interestingly if I remove *s=*s1 and print s1 instead the program works. But I haven't even set the "\0" character at the end of s1 so how does it even know where to stop printing?
And in the case below what exactly is the issue?
#include <stdio.h> #include <string.h> #include <stdlib.h> void main(void) { char *s = "abcdefghijklmnop", *s1=malloc(17); int i; for(i=0;i<strlen(s);i++) { *(s1+i) = *(s+strlen(s)-1-i); } *s=*s1; printf("%s",s); }
*s = *s1ands = s1is?*(p + i)for some pointerpand indexi, just writep[i]instead. It's much easier to read, and means the exact same thing.