C 奇怪的指针算术

标签 c pointers

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

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/

相关文章:

javascript - 是否可以将光标轨迹添加到网页,而不是默认轨迹,而是图片或 gif?

c - 我刚刚开始学习C : Can someone explain what the pointers and typecasting are doing in this code?

c - 如何比较 switch-case 中的枚举值?

c++ - 在静态函数中调用成员函数指针

C 编程 - 套接字 ("Server"在聊天中显示 "Client"名称)

java - Linux/X Window 系统下的屏幕阅读/鼠标点击?

c - 将指针设置为 NULL 会产生 "assignment makes integer from pointer without a cast"警告

c - 不知道如何实现指向 char 指针数组的指针

c - yacc - 字段的类型不完整

c - 为堆栈上的变量声明指针有什么意义吗?