当铸造指针的增量操作者会发生什么? [英] What happens when a casted pointer has an increment operator?
本文介绍了当铸造指针的增量操作者会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
int x[100];
void *p;
x[0] = 0x12345678;
x[1] = 0xfacecafe;
x[3] = 0xdeadbeef;
p = x;
((int *) p) ++ ;
printf("The value = 0x%08x", *(int*)p);
编译上面与++运算符的行生成一个左值所需的错误。
Compiling the above generates an lvalue required error on the line with the ++ operator.
推荐答案
演员创建了一个类型的临时指针为int *
。可以不增加临时因为它不表示存储结果的地方。
The cast creates a temporary pointer of type int *
. You can't increment a temporary as it doesn't denote a place to store the result.
在C和C ++ standardese,(INT *)P
是右值,大致意思是前pression,只有能发生在赋值的右手边。
In C and C++ standardese, (int *)p
is an rvalue, which roughly means an expression that can only occur on the right-hand side of an assignment.
P
,另一方面是一个左值,这意味着它可以有效地出现在赋值的左侧。只有左值可以增加。
p
on the other hand is an lvalue, which means it can validly appear on the left-hand side of an assignment. Only lvalues can be incremented.
这篇关于当铸造指针的增量操作者会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文