Error while writing strcat() using pointers

前端 未结 4 823
忘了有多久
忘了有多久 2021-01-15 22:52

I am trying to learn C with The C programming Language by K&R. I am trying to write a the strcat() program using pointers.

char         


        
4条回答
  •  孤城傲影
    2021-01-15 23:43

    Because the pointers are pointers to literals, and not only are those read only the destination is not big enough to contain both the strings. For at least string1 you would want to use an array of at least big enough size to contain both strings (including the terminator). Like:

    char string1[128] = "Stack";
    

    or

    char string1[128];
    strcpy(string1, "Stack");
    

提交回复
热议问题