C appending char to char*

Posted by Ostap Hnatyuk on Stack Overflow See other posts from Stack Overflow or by Ostap Hnatyuk
Published on 2012-10-17T16:53:25Z Indexed on 2012/10/17 17:01 UTC
Read the original article Hit count: 182

Filed under:
|
|
|
|

So I'm trying to append a char to a char*.

For example I have char *word = " "; I also have char ch = 'x';

I do append(word, ch); Using this method..

void append(char* s, char c)
{

    int len = strlen(s);
    s[len] = c;
    s[len+1] = '\0';
}

It gives me a segmentation fault, and I understand why I suppose. Because s[len] is out of bounds. How do I make it so it works? I need to clear the char* a lot as well, if I were to use something like char word[500]; How would I clear that once it has some characters appended to it? Would the strlen of it always be 500? Thanks in advance.

© Stack Overflow or respective owner

Related posts about c

    Related posts about string