【发布时间】:2016-04-07 22:04:37
【问题描述】:
char *s;
char buf [] = "This is a test";
s = strchr (buf, 't');
if (s != NULL)
printf ("found a 't' at %s\n", s);
printf("%c\n",*s);
printf("%c\n",*s++);
printf("%c\n",*s++);
printf("%c\n",*s++);
printf("%c\n",*s++);
此代码输出:
found a 't' at test
t
t
e
s
t
Program ended with exit code: 0
在我看来,*s 应该是t,*s++ 应该是e。但是为什么它们在这段代码中具有相同的值?
【问题讨论】:
-
“你的观点”是基于什么吗?
-
后增量意味着执行然后增量。试试 *(++s)
-
s 表示字符串 s 的起始地址,所以 *s 应该是地址中存储的值。 s++ 应该是从字符串 s 的起始地址开始的下一个位置。
-
也许你应该读一本 C 书?