字符串和函数返回值是lvalue还是rvalue ?

时间:2022-12-07 22:28:18
  1. Just wonder if a literal string is an lvalue or an rvalue. Are other literals (like int, float, char etc) lvalue or rvalue?

    只是想知道文字字符串是lvalue还是rvalue。其他文字(如int、float、char等)是lvalue还是rvalue?

  2. Is the return value of a function an lvalue or rvalue?

    函数的返回值是lvalue还是rvalue?

How do you tell the difference?

你怎么区分呢?

3 个解决方案

#1


40  

  1. string literals are lvalues, but you can't change them
  2. 字符串常量是lvalue,但是您不能更改它们
  3. rvalue, but if it's a pointer and non-NULL, the object it points to is an lvalue
  4. rvalue,但如果它是一个指针和非空值,它指向的对象是一个lvalue。

The C standard recognizes the original terms stood for left and right as in L = R; however, it says to think of lvalue as locator value, which roughly means you can get the address of an object and therefore that object has a location. (See 6.3.2.1 in C99.)

C标准承认原来的术语代表左和右,如L = R;但是,它认为lvalue是定位器值,这大致意味着您可以获得一个对象的地址,因此该对象有一个位置。(见6.3.2.1 C99中)。

By the same token, the standard has abandoned the term rvalue, and just uses "the value of an expression", which is practically everything, including literals such as ints, chars, floats, etc. Additionally, anything you can do with an rvalue can be done with an lvalue too, so you can think of all lvalues as being rvalues.

同样,标准已经放弃了右值,就使用一个表达式的值,这是几乎所有的东西,包括文字,如整数、字符、花车、等等。此外,任何你可以做一个右值也可以用一个左值,所以你能想到的所有左值是右值。

#2


4  

There are two kinds of expressions in C: 1. lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment. 2. rvalue: An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and cannot appear on the left-hand side. Following is a valid statement: int g = 20; But following is not a valid statement and would generate compile-time error: 10=20;

在C中有两种表达式:1。lvalue:一个lvalue的表达式可以出现在赋值的左边或右边。2。rvalue: rvalue的表达式可能出现在赋值的右边,但不是左边。变量是lvalues,因此可能出现在赋值的左侧。数值文字是rvalue,因此可能不会被赋值,也不会出现在左手边。下面是一个有效的语句:int g = 20;但是,下面并不是一个有效的语句,会产生编译时错误:10=20;

#3


1  

there's a definition for C++ from Microsoft. By this definition, a literal string, say "hello world", is lvalue, because it's const and not temporary. Actually it persists across your application's lifetime.

有一个来自微软的c++定义。根据这个定义,文字字符串“hello world”是lvalue,因为它是const而不是临时的。实际上,它在应用程序的整个生命周期中都持续存在。

#1


40  

  1. string literals are lvalues, but you can't change them
  2. 字符串常量是lvalue,但是您不能更改它们
  3. rvalue, but if it's a pointer and non-NULL, the object it points to is an lvalue
  4. rvalue,但如果它是一个指针和非空值,它指向的对象是一个lvalue。

The C standard recognizes the original terms stood for left and right as in L = R; however, it says to think of lvalue as locator value, which roughly means you can get the address of an object and therefore that object has a location. (See 6.3.2.1 in C99.)

C标准承认原来的术语代表左和右,如L = R;但是,它认为lvalue是定位器值,这大致意味着您可以获得一个对象的地址,因此该对象有一个位置。(见6.3.2.1 C99中)。

By the same token, the standard has abandoned the term rvalue, and just uses "the value of an expression", which is practically everything, including literals such as ints, chars, floats, etc. Additionally, anything you can do with an rvalue can be done with an lvalue too, so you can think of all lvalues as being rvalues.

同样,标准已经放弃了右值,就使用一个表达式的值,这是几乎所有的东西,包括文字,如整数、字符、花车、等等。此外,任何你可以做一个右值也可以用一个左值,所以你能想到的所有左值是右值。

#2


4  

There are two kinds of expressions in C: 1. lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment. 2. rvalue: An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and cannot appear on the left-hand side. Following is a valid statement: int g = 20; But following is not a valid statement and would generate compile-time error: 10=20;

在C中有两种表达式:1。lvalue:一个lvalue的表达式可以出现在赋值的左边或右边。2。rvalue: rvalue的表达式可能出现在赋值的右边,但不是左边。变量是lvalues,因此可能出现在赋值的左侧。数值文字是rvalue,因此可能不会被赋值,也不会出现在左手边。下面是一个有效的语句:int g = 20;但是,下面并不是一个有效的语句,会产生编译时错误:10=20;

#3


1  

there's a definition for C++ from Microsoft. By this definition, a literal string, say "hello world", is lvalue, because it's const and not temporary. Actually it persists across your application's lifetime.

有一个来自微软的c++定义。根据这个定义,文字字符串“hello world”是lvalue,因为它是const而不是临时的。实际上,它在应用程序的整个生命周期中都持续存在。