strcat concat a char onto a string?

后端 未结 6 740
谎友^
谎友^ 2020-12-14 22:45

Using GDB, I find I get a segmentation fault when I attempt this operation:

strcat(string,¤tChar);

Given that string is initializ

6条回答
  •  情深已故
    2020-12-14 23:15

    strcat() takes two '\0'-terminated strings. When you pass the address of a character, the routine will look at the memory that follows the character, looking for the terminator.

    Since you don't know what that memory even refers to, you should expect problems when your code accesses it.

    In addition to that, your string argument does not have room to have any characters appended to it. Where is that memory written to? It will attempt to write past the end of the memory associated with this string.

提交回复
热议问题