Reverse a c style string - runtime error -
i've got code try reverse c-style string, getting runtime error , use figuring out why. thanks!
void switchedstr(char * str) { char * end = str; if (str) { while (*end) { ++end; } end--; } char temp; while (end > str) { temp = *str; *str = *end; *end = temp; end--; str++; } }
if you're passing
char *str = "test123";
to switchedstr
, give access violation, since such strings constant , compiler puts them in non-writable location (see section 6.4.5 of c99). if try modify it, you'd undefined behavior. however, if it's not constant string literal, it'd work, example
char str[]="test123";
or
char *str = _strdup("test123");
Comments
Post a Comment