Not null-terminating a C-style string

后端 未结 6 2075
萌比男神i
萌比男神i 2021-01-24 12:08

Given a string , say ,

char *str = \"Hello,StackOverflow!\"
char newStr[30];
int l = strlen(str);
for(int i =0 ; i

        
6条回答
  •  时光取名叫无心
    2021-01-24 12:28

    Your program has undefined behaviour, since you promise to call printf with a pointer to a null-terminated string but fail to do so. Anything could happen, but your program is simply not correct.

    Specifically, while reading the array elements one by one to find the null terminator, the program will eventually access an uninitialized variable, which is UB.

提交回复
热议问题