why the string is getting altered after strcat()?

前端 未结 4 624
一向
一向 2021-01-27 22:20

this is the source code

int main()
{
    char str[]=\"dance\";
    char str1[]=\"hello\";
    char str2[]=\"abcd\";
    strcat(str1,str2);
    printf(\"%s\",s         


        
4条回答
  •  故里飘歌
    2021-01-27 22:44

    I got it...

    as I have not given the size of str1 , both str1 and str are present in the memory one after another

    like

    h e l l o \0 d a n c e
    

    so when I concatenate str1 and str2 following thing happens...

    a replaces \0
    b replaces d
    c replaces a
    d replaces n
    \0 replaces c
    

    and hence str is altered

提交回复
热议问题