So I\'m trying to append a char
to a char*
.
For example I have char *word = \" \";
I also have char ch = \'x\';
>
It is hard to append to a string in-place in C. Try something like this:
char *append(const char *s, char c) {
int len = strlen(s);
char buf[len+2];
strcpy(buf, s);
buf[len] = c;
buf[len + 1] = 0;
return strdup(buf);
}
Be sure to deallocate the returned string when done with it.
FYI: It segfaults probably because the string you are passing is stored in read-only memory. But you're right, you are also writing off of the end (the [len+1]
write, not the [len]
one).