gpt4 book ai didi

C 奇怪的指针算术

转载 作者:行者123 更新时间:2023-11-30 20:13:21 24 4
gpt4 key购买 nike

我有一个很愚蠢的问题,我无法回答。有人可以告诉我为什么下面的代码有效吗?

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com