📄 lvalues.html
字号:
<html lang="en">
<head>
<title>Using the GNU Compiler Collection (GCC)</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
<p>Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Funding
Free Software", the Front-Cover texts being (a) (see below), and with
the Back-Cover Texts being (b) (see below). A copy of the license is
included in the section entitled "GNU Free Documentation License".
<p>(a) The FSF's Front-Cover Text is:
<p>A GNU Manual
<p>(b) The FSF's Back-Cover Text is:
<p>You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development.-->
</head>
<body>
<div class="node">
<p>
Node:<a name="Lvalues">Lvalues</a>,
Next:<a rel="next" accesskey="n" href="Conditionals.html#Conditionals">Conditionals</a>,
Previous:<a rel="previous" accesskey="p" href="Typeof.html#Typeof">Typeof</a>,
Up:<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a>
<hr><br>
</div>
<h3 class="section">Generalized Lvalues</h3>
<p>Compound expressions, conditional expressions and casts are allowed as
lvalues provided their operands are lvalues. This means that you can take
their addresses or store values into them.
<p>Standard C++ allows compound expressions and conditional expressions as
lvalues, and permits casts to reference type, so use of this extension
is deprecated for C++ code.
<p>For example, a compound expression can be assigned, provided the last
expression in the sequence is an lvalue. These two expressions are
equivalent:
<pre class="example"> (a, b) += 5
a, (b += 5)
</pre>
<p>Similarly, the address of the compound expression can be taken. These two
expressions are equivalent:
<pre class="example"> &(a, b)
a, &b
</pre>
<p>A conditional expression is a valid lvalue if its type is not void and the
true and false branches are both valid lvalues. For example, these two
expressions are equivalent:
<pre class="example"> (a ? b : c) = 5
(a ? b = 5 : (c = 5))
</pre>
<p>A cast is a valid lvalue if its operand is an lvalue. A simple
assignment whose left-hand side is a cast works by converting the
right-hand side first to the specified type, then to the type of the
inner left-hand side expression. After this is stored, the value is
converted back to the specified type to become the value of the
assignment. Thus, if <code>a</code> has type <code>char *</code>, the following two
expressions are equivalent:
<pre class="example"> (int)a = 5
(int)(a = (char *)(int)5)
</pre>
<p>An assignment-with-arithmetic operation such as <code>+=</code> applied to a cast
performs the arithmetic using the type resulting from the cast, and then
continues as in the previous case. Therefore, these two expressions are
equivalent:
<pre class="example"> (int)a += 5
(int)(a = (char *)(int) ((int)a + 5))
</pre>
<p>You cannot take the address of an lvalue cast, because the use of its
address would not work out coherently. Suppose that <code>&(int)f</code> were
permitted, where <code>f</code> has type <code>float</code>. Then the following
statement would try to store an integer bit-pattern where a floating
point number belongs:
<pre class="example"> *&(int)f = 1;
</pre>
<p>This is quite different from what <code>(int)f = 1</code> would do--that
would convert 1 to floating point and store it. Rather than cause this
inconsistency, we think it is better to prohibit use of <code>&</code> on a cast.
<p>If you really do want an <code>int *</code> pointer with the address of
<code>f</code>, you can simply write <code>(int *)&f</code>.
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -