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);
编译上述代码会在带有 ++ 运算符的行上生成一个 lvalue required 错误.
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++ 标准中,(int *)p 是一个 rvalue,大致表示只能出现在赋值右侧的表达式.
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.
这篇关于当强制转换的指针具有增量运算符时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当强制转换的指针具有增量运算符时会发生什么?
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
