我有一个很愚蠢的问题,我无法回答。 有人可以告诉我为什么下面的代码有效吗?
char hello[]="Hello World\n";
char *hptr = hello;
while(*hptr)
{
printf("%c", *hptr++);//here the output must be "ello World", but C thinks otherwise!!!
}
最佳答案
您正在使用后增量:
*hptr++
这首先使用 hptr
的值,然后递增它。如果您想跳过第一个字母,您可以使用预增量:
*++hptr
这会增加指针值,然后将其用作函数参数。
关于C 奇怪的指针算术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318566/