You code could be segfaulting for a number of reasons. Here are the ones that come to mind
- s is NULL
- s points to a const string which is held in read only memory
- s is not NULL terminated
I think #2 is the most likely. Can you show us the call site of reverse?
EDIT
Based on your sample #2 is definitely the answer. A string literal in C/C++ is not modifiable. The proper type is actually const char*
and not char*
. What you need to do is pass a modifiable string into that buffer.
Quick example:
char* pStr = strdup("foobar");
reverse(pStr);
free(pStr);