c - L值到R值的转换

标签 c expression lvalue-to-rvalue

这里的网站说: http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc05lvalue.htm

If an lvalue appears in a situation in which the compiler expects an rvalue, 
the compiler converts the lvalue to an rvalue.

An lvalue e of a type T can be converted to an rvalue if T is not a function or        
array type. The type of e after conversion will be T. 

Exceptions to this is:

    Situation before conversion             Resulting behavior

1) T is an incomplete type                  compile-time error
2) e refers to an uninitialized object      undefined behavior
3) e refers to an object not of type T      undefined behavior

问题 1:

考虑以下程序,

int main()
{   
    char p[100]={0};      // p is lvalue
    const int c=34;       // c non modifiable lvalue

    &p; &c;               // no error fine beacuse & expects l-value
    p++;                  // error lvalue required  

    return 0;
}

我的问题是,为什么在表达式 (p++) ++(postfix) 中期望 l-values 而数组是 l -value 那为什么会出现这个错误呢? gcc 错误:需要左值作为递增操作数|

问题 2:

请用例子解释异常3

最佳答案

数组确实是左值,但它们是不可修改的。标准说:

6.3.2.1

A modifiable lvalue is an lvalue that does not have array type

关于c - L值到R值的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547174/

相关文章:

c++ - 左值到右值的转换

使用avr控制多个 Helm 机

c - 在没有堆栈的情况下处理 boolean 表达式?

c - 当我们声明双指针(**q)时int/char到底是什么

r - 绘图标签中的表达式和新行

c# - 添加两个表达式以在 Entity Framework Core 3 中创建谓词不起作用

c++ - 数组和右值

c - 如何使用 TerminateProcess 终止进程

c - 段错误处理程序是否应该由应用程序程序员用 C 语言编写?