我试图理解为什么下面的代码会产生 BCD123 123 的输出。
void f(char *p)
{
*p += 1;
}
int main()
{
int i;
char a[] = "ABC" "123";
char *p = a;
for (i = 0; i < 3; i++)
f(p++);
printf("%s ", a);
printf("%s ", p);
return 0;
}
最佳答案
void f(char *p)
{
*p += 1;
}
将 1 添加到传入的字符。'A' + 1 = 'B' 等。查看 ascii 表 http://www.asciitable.com/
第 2 部分
char a[] = "ABC" "123";
// is the same as
char a[] = "ABC123";
for (i = 0; i < 3; i++)
f(p++); <<<<===== moves p along the string 3 places (once for each loop)
printf("%s ", a);
printf("%s ", p); <<< p now points at 4th char
关于c - C 中的指针和字符串输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29088648/