C 中的指针和字符串输出
Pointer and String Outputs in C
我试图理解为什么以下代码会产生 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 table 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
我试图理解为什么以下代码会产生 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 table 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