注意 * 和 ++ 的优先级在 C/C++ 中,(以及编程时的任何击键)
有人写这个函数
void strToUpper(char *p) {
while (*p) {
*p = TOUPPER(*p);
*p++; //<-- Line to pay attention
}
}
我问,为什么把*放在p++前面?
答案:因为“是一样的”,我纠正了代码,然后生气了一段时间,因为两者的工作原理确实相同...
void strToUpper(char *p) {
while (*p) {
*p = TOUPPER(*p);
p++; //<-- Line to pay attention
}
}
所以我想与 stackoverflow 分享这一点,例如:
char s[6]="hello ”;
它会做什么?
*++ptr;
这将评估 ++ 预自增(通过指针),然后评估取消引用运算符 *,因此它将让 char 值“e”(“hello”的第二个字母)(在本例中未使用,并且可以生成编译警告)并且指针将从“e”(位置 1)指向
它会做什么?
*ptr++;
这有点奇怪,因为它会首先评估解引用运算符 * ,所以它会让 char 值“h”(在本例中都没有使用),然后是 ++ 后递增(到指针),所以(再次)指针将从“e”(位置 1)指向
它会做什么?
ptr++;
最后它不会有 char 的左值,但如果不使用它不会生成任何警告,并且指针也将从“e”(位置 1)指向。
从指针地址的角度来看,这三种形式的作用是相同的。
恕我直言,这是某些计算机语言(几乎所有人)中最糟糕的事情。
“任何代码和任何错误之间的汉明距离都很差”
我们在编程时没有冗余,如果你拿一本法律书,并在其中写入随机字符,它将是可读的,但如果你在编程时输入随机,你会得到一个错误,100%的准确性
Somebody write this function
void strToUpper(char *p) {
while (*p) {
*p = TOUPPER(*p);
*p++; //<-- Line to pay attention
}
}
I asked, why do you put the * before p++?
answer: because "It's the same", I corrected the code and then stay angry for a while, because both really work the same...
void strToUpper(char *p) {
while (*p) {
*p = TOUPPER(*p);
p++; //<-- Line to pay attention
}
}
So I would like to share this with stackoverflow, example:
char s[6]="hello";
What would it do?
*++ptr;
This will evaluate the ++ pre-increment(over the pointer) and then the dereference operator *, so it will let a char value 'e' (second letter of "hello") (that is not used in this case and could generate a warning on compilation) and the pointer will be pointing from the 'e' (position 1)
What would it do?
*ptr++;
This is a little weird because it will evaluate the dereference operator * first, so it will let a char value 'h' (that is neither used in this case), and then the ++ post-increment(to the pointer), so (again) the pointer will be pointing from the 'e' (position 1)
What would it do?
ptr++;
Finally it won't have a Lvalue of char, but it won't generate any warning if isn't used, and the pointer will be pointing from the 'e' (position 1) too.
These three forms does the same from the pointer address point of view..
IMHO That's the worst thing of some computer languages (almost everyone)..
"There is a poor Hamming distance between any code and any bug"
We have no redundancy when programming, if you take a Law book, and write random chars within, It will be readable, but if you type random when programming, you get a bug, 100% accuracy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
++ 优先于 *。但我同意,不清楚何时将两者混合在同一个声明中。
++ has precedence over *. But I agree, it's unclear when you mix both in the same statement.
根据 C 中的运算符优先级,
++
将首先计算,然后才是*
。因此,在 *p++ 中,ptr 的值将递增,然后该语句将获取 ptr 现在指向的 opbject 的值到。当然,这与 ptr++ 没有什么不同,因为您没有使用语句的值。According to the operator precedence in C,
++
will evaluate first, and only then*
. So in*p++
,ptr
will get its value inceremented, and then the statement will get the value of the opbject to whichptr
is now pointing to. Of course, this isn't different fromptr++
, since you aren't using the value of the statement.任何将 ++、--、+= 等与其他运算符混合使用的人都已经无法编写可读的代码,事实上,他们将错误排除在外,这只不过是他们应得的。
Anyone who mixes ++, --, += etc with other operators has already failed writing readable code, the fact that they'll bug it out is nothing more than their due.