需要解释以下代码段的左值分配 [英] Need Explanation on Lvalue Assignment for the following snippet

查看:69
本文介绍了需要解释以下代码段的左值分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< conio.h>

int main()

{

const int i = 6;

int k = 9;

int a [i],* c = a,* v = a;

a [2] = 9;


* c ++ = * v ++; //(1)这一行不会产生任何异常/

错误

k ++ = k ++; //(2)但这一行正在产生

k ++ = 6; //(3)这也产生了


getch();

}


现在我知道你了无法为表达式赋值;但为什么

上面的(1)表达式没有产生异常/错误/

警告? (2)& (3)按照预期生成。请

解释一下。

这是由于案例(1)中的运算符优先级,即表达式

访问该值然后分配他们然后递增指针

并继续前进。如果我错了,请更正..


(我正在使用DevC ++编译器进行测试)

#include<stdio.h>
#include<conio.h>
int main()
{
const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /
error
k++ = k++; // ( 2 ) But this line is generating
k++ = 6; // ( 3 ) This Also Generating

getch();
}

Now I know that you cannot assign a value to an expression; But why
does the above (1) expression not generating a exception/error/
warning? and (2) & (3) are generating as they are supposed to. Please
explain this.
Is this due to operator precedence in the case(1), ie the expression
access the value and then assign them and then increment the pointers
and move forward. Correct if I am wrong..,

(I am using DevC++ Compiler for testing)

推荐答案

Pranav写道:
Pranav wrote:

#include< stdio.h>

#include< conio.h>

int main()

{

const int i = 6;

int k = 9;

int a [ i],* c = a,* v = a;

a [2] = 9;


* c ++ = * v ++; //(1)这一行不会产生任何异常/
#include<stdio.h>
#include<conio.h>
int main()
{
const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /



你在那里给(* c)赋值。

没有错。

You''re assigning a value to (*c) there.
There nothing wrong with that.


error

k ++ = k ++; //(2)但这一行正在产生

k ++ = 6; //(3)这也生成
error
k++ = k++; // ( 2 ) But this line is generating
k++ = 6; // ( 3 ) This Also Generating



虽然(k ++)具有相同的值

,因为(k)在增量之前做了,

(k ++)是算术运算的结果。


同样问题的一个更简单的例子是:


(k + 1)= 6;


(& k)是(k)的地址,但是(k + 1)没有地址,

所以没有地方可以把(6)的值放进去。


-

pete

Though (k++) has the same value
as (k) did before the increment,
(k++) is the result of an arithmetic operation.

A simpler example of the same problem, would be:

(k + 1) = 6;

(&k) is the address of (k), but (k + 1) doesn''t have an address,
so there is no place to put the value of (6) into.

--
pete


pete说:
pete said:

Pranav写道:
Pranav wrote:



< ;剪断>

<snip>


> const int i = 6;
int k = 9;
int a [i] ,* c = a,* v = a;
a [2] = 9;

* c ++ = * v ++; //(1)这一行不会产生任何异常/
>const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /



你在那里给(* c)赋值。

那没有错。


You''re assigning a value to (*c) there.
There nothing wrong with that.



再看一遍! :-)


< snip>


-

Richard Heathfield< http:// www .cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Look again! :-)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Richard Heathfield写道:
Richard Heathfield wrote:

pete说:
pete said:

> Pranav写道:
>Pranav wrote:



< snip>

<snip>


>> const int i = 6;
int k = 9;
int a [ i],* c = a,* v = a;
a [2] = 9;

* c ++ = * v ++; //(1)这一行不会产生任何异常/
>>const int i=6;
int k = 9;
int a[i], *c = a, *v = a ;
a[2] = 9;

*c++ = *v++; // ( 1 ) This line does not generate any exception /


你在那里给(* c)赋值。
没有任何问题。

You''re assigning a value to (*c) there.
There nothing wrong with that.



再看一遍! :-)


Look again! :-)



我只是从主题行解决左值问题。

代码也存在其他问题。

我在这个帖子中读到了你的其他帖子。

我想你已经把它们全部收了。


-

皮特

I was just addressing the lvalue issue from the subject line.
The code has other problems too.
I read you other post in this thread.
I think you got them all.

--
pete


这篇关于需要解释以下代码段的左值分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆