为什么程序会出现
char *s, *p, c;
s = "abc";
printf(" Element 1 pointed to by S is '%c'\n", *s);
printf(" Element 2 pointed to by S is '%c'\n", *s+1);
printf(" Element 3 pointed to by S is '%c'\n", *s+2);
printf(" Element 4 pointed to by S is '%c'\n", *s+3);
printf(" Element 5 pointed to by S is '%c'\n", s[3]);
printf(" Element 4 pointed to by S is '%c'\n", *s+4);
给出以下结果?
Element 1 pointed to by S is 'a'
Element 2 pointed to by S is 'b'
Element 3 pointed to by S is 'c'
Element 4 pointed to by S is 'd'
Element 5 pointed to by S is ' '
Element 4 pointed to by S is 'e'
编译器是如何继续序列的?为什么
s[3]
返回一个空值?
*(s+1)
等,但*(s+4)
将是未定义的行为。 - Daniel Fischers[3]
返回第四个字符(它们从0开始索引),这是一个字符串终止符\0
。 - Joachim Isakssons[3]
是\0
,所以它不显示任何内容。 - bash.dprintf
。std::cout
(来自头文件<iostream>
)更安全、更易于使用且更少出错。 - Violet Giraffe