假设char*p=";“你好,世界”;,那么有没有'\0';就在'之后存在;d';? #包括 #包括 int main() { char*p=“你好,世界”; inta=(int)(*(p+strlen(p)+1));//等于a=(int)(*p+12) printf(“%d\n”,a); 返回0; }
a不是“\0”,为什么假设char*p=";“你好,世界”;,那么有没有'\0';就在'之后存在;d';? #包括 #包括 int main() { char*p=“你好,世界”; inta=(int)(*(p+strlen(p)+1));//等于a=(int)(*p+12) printf(“%d\n”,a); 返回0; },c,C,a不是“\0”,为什么 #include<stdio.h> #include<string.h> int main() { char *p = "hello world"; int a = (int)(*(p+strlen(p)+1)); // equal to a=(int)(*p+12) printf("%d\n", a); return 0; } #包括 #包括 int main() { char*p=“你好,世界”; i
#include<stdio.h>
#include<string.h>
int main()
{
char *p = "hello world";
int a = (int)(*(p+strlen(p)+1)); // equal to a=(int)(*p+12)
printf("%d\n", a);
return 0;
}
#包括
#包括
int main()
{
char*p=“你好,世界”;
inta=(int)(*(p+strlen(p)+1));//等于a=(int)(*p+12)
对于(int i=0;p[i]!='\0';i++)
printf(“%c”,p[i]);
返回0;
}
返回:你好,世界
因此,我想知道字符串末尾是否存在“\0”?您以1为单位,读取nul终止符以外的1字节的值。试一试
#include<stdio.h>
#include<string.h>
int main()
{
char *p = "hello world";
int a = (int)(*(p+strlen(p)+1)); // equal to a=(int)(*p+12)
for(int i=0; p[i]!='\0'; i++)
printf("%c",p[i]);
return 0;
}
相反您被1淘汰,读取nul终止符之外1字节的值。试一试
#include<stdio.h>
#include<string.h>
int main()
{
char *p = "hello world";
int a = (int)(*(p+strlen(p)+1)); // equal to a=(int)(*p+12)
for(int i=0; p[i]!='\0'; i++)
printf("%c",p[i]);
return 0;
}
int a = *(p+strlen(p));
反而
int a = *(p+strlen(p));
你又多了一个
你需要这样定义
int a = (int)(*(p+strlen(p)+1));
让我看看这个
int a = (int)(*(p+strlen(p))); //now a consists 0 integer value of null
编辑:C-初始化直接字符串时隐式添加“\0”。
但是当你静态初始化的时候,你会发现一些不同
这两条弦相等
char *p ="abc";
p ==>a
p+1 ==>b
p+2 ==>c
p+3 ==>null
这些都不是
char *p = "helloworld";
char q[]={'h','e','l','l','o','w','o','r','l','d','\0'};
用此按钮观察结果
char *p = "helloworld";
char q[]={'h','e','l','l','o','w','o','r','l','d'};
你又多了一个
你需要这样定义
int a = (int)(*(p+strlen(p)+1));
让我看看这个
int a = (int)(*(p+strlen(p))); //now a consists 0 integer value of null
编辑:
C-初始化直接字符串时隐式添加“\0”。
但是当你静态初始化的时候,你会发现一些不同
这两条弦相等
char *p ="abc";
p ==>a
p+1 ==>b
p+2 ==>c
p+3 ==>null
这些都不是
char *p = "helloworld";
char q[]={'h','e','l','l','o','w','o','r','l','d','\0'};
用此按钮观察结果
char *p = "helloworld";
char q[]={'h','e','l','l','o','w','o','r','l','d'};
在您的情况下,\0位于*p之后11个槽口,而不是12个槽口。在您的情况下,\0位于*p之后11个槽口,而不是12个槽口。要回答实际问题,是的,C总是在字符串文本中添加终止的\0
。要回答实际问题,是的,C总是在字符串文本中添加终止的\0
。如果使用简单字符串,则是。像字符a[6]={'H','e','l','l','o','\0'};
您需要在外部以直接字符串或指针变量编译器的形式写入空指针。如果使用简单字符串,则会自动执行此操作。是。像字符a[6]={'H','e','l','l','o','\0'};
您需要在外部以直接字符串或指针变量编译器的形式写入空指针。是的,我故意这样做是为了知道“d”后面的字符是什么。所以我的意思是,如果字符串的末尾确实存在'\0',那么字符串的末尾就有一个nul终止符。您的代码读取的字节太多;我建议的改动是读《终结者》,我故意这样做是为了知道“d”后面的字符是什么。所以我的意思是,如果字符串的末尾确实存在'\0',那么字符串的末尾就有一个nul终止符。您的代码读取的字节太多;我建议的更改将读取终止符,因此我的意思是,如果字符串末尾确实存在“\0”。它是“hello world”或“h”“e”“l”“l”“o”“w”“o”“r”“l”“d”“\0”“?@user2046815在答案中添加了一些更多信息。可能会有所帮助。解释您需要的内容。在示例中,p
是有效的C字符串,但q
不是有效的C字符串,这正是因为它不是以0字节结尾的strcmp
需要有效的C字符串,使用它来比较p
和q
,这里的结果是。内存中“q
之后”的字节可以是零,也可以是任何其他随机值,strcmp
的结果无法预测。@Jongware非常感谢您发表此有用的评论。是的,这将给出未定义的行为。我也被测试过。我被作为一个例子来展示使用字符串和使用单个字符定义之间的一些区别。所以我的意思是,如果字符串的末尾确实存在'\0'。它是“hello world”或“h”“e”“l”“l”“o”“w”“o”“r”“l”“d”“\0”“?@user2046815在答案中添加了一些更多信息。可能会有所帮助。解释您需要的内容。在示例中,p
是有效的C字符串,但q
不是有效的C字符串,这正是因为它不是以0字节结尾的strcmp
需要有效的C字符串,使用它来比较p
和q
,这里的结果是。内存中“q
之后”的字节可以是零,也可以是任何其他随机值,strcmp
的结果无法预测。@Jongware非常感谢您发表此有用的评论。是的,这将给出未定义的行为。我也被测试过。我被作为一个例子来展示用字符串定义和用单个字符定义之间的区别。谢谢你们的帮助。完全有道理。事实上,我已经超过“两个”了!谢谢你的帮助。完全有道理。事实上,我已经超过“两个”了!