[英]Null-Terminated string
以下哪個是以空字符結尾的字符串?
char *str1 = "This is a string.";
char *str2 = "This is a string.\0";
char str3[] = "This is a string.";
const char *str4 = "This is a string.";
const char *str5 = "This is a string.\0";
const char str6[] = "This is a string.";
str2
和str5
具有雙重空終止字符串的特殊性 另外:
char *str1
應該是const char *str1
char *str2
應該是const char *str2
它們都是空終止的( str2
和str5
實際上是雙空終止的),因為使用雙引號是空終止的char數組的簡寫。
所以這:
"Hello"
實際上是這樣的:
{'H', 'e', 'l', 'l', 'o', '\0'}
指向字符串文字的變量應該聲明為const
。
所有。 C編譯器自動聚合帶有終止'\\ 0'的字符串,然后將其存儲在char []中或通過char *引用它;
不要使用char*s="hi"
,以防你想對字符串做一些修改,或者甚至想要連接它的東西,因為這里字符串hi
是在內存的只讀部分,你的程序可以緊急
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.