Null-Terminated string

后端 未结 4 1387
一个人的身影
一个人的身影 2021-01-17 15:26

Which of the following are null-terminated string?

char *str1 = \"This is a string.\";
char *str2 = \"This is a string.\\0\";
char str3[] = \"This is a strin         


        
4条回答
  •  天涯浪人
    2021-01-17 16:13

    They are all null-terminated (str2 and str5 are actually doubly-null-terminated) because using double quotes is a shorthand for a null-terminated char array.

    So this:

    "Hello"
    

    Is actually this:

    {'H', 'e', 'l', 'l', 'o', '\0'}
    

    Variables pointing to string literals such as these should be declared as const.

提交回复
热议问题