2013-08-31 75 views
0

這裏的網站說: http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc05lvalue.htmL-值R值轉換

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 

Ques.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錯誤:所需的左值爲增量操作數|

Ques.2:

Plzz解釋exception 3example

+0

該數組的地址(靜態)不是一個l值。 –

+0

我不明白你的問題(#1)與你引用的段落有什麼關係。使用後綴'++'不涉及從左值到右值的轉換。 – nickie

+0

@nickie當我編譯這個語句p ++時,我得到gcc的編譯時錯誤 – Duggs

回答

3

數組確實是左值,但它們不可修改。該標準說:

6.3.2.1

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

+0

時確切發生的是什麼此外,6.5.2.4,後綴++的操作數應該是一個可修改的左值。正確回答你的'p'不是可修改的左值。 – nickie

1

的回答質疑2.

說你有double類型的對象。你拿一個指針並將其轉換爲不同的指針類型。然後你使用新的指針來解引用對象。這是未定義的行爲。

double x = 42.0; 
double *p = &x; 
int *q = (int *) p; 

*q; 

這裏,*qint類型的左值不指int類型的對象。