无法理解错误条件WRT左值 [英] Not able to understand error condition wrt lvalues

查看:76
本文介绍了无法理解错误条件WRT左值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,正在尝试一些组合。

 #include< stdio.h> 


int main()
{
int a = 5;

printf(%d,& a); //陈述1
printf( \n%d,a); //语句2
printf( \n%d,&(a ++)); //声明3
printf( \n%d,a); //声明4

返回0;
}

我在陈述3中说了一个错误


[错误]值必须为一元'&'操作数


I预期STATEMENT 1& 3和两个地址相同。



我还希望STATEMENT 2的输出为5&。语句4为6。 -error>左值必填错误



我理解该问题。从评论到上述问题的第一个答案,我认为左值是某种东西



但是我仍然不明白为什么&(a ++)或&(++ a)应该给出错误。
任何帮助将不胜感激。



谢谢您的阅读。



,谢谢您的回答。如果可能,请提供参考,其中将讨论确切的执行顺序或此类表达式的性质。
这样,像我这样的新手就不会在社区中遇到如此琐碎的问题。

解决方案

a ++ 递增您的 a 并返回 old a & 运算符返回变量的地址。但是返回的 old a 没有地址。这是非常临时的事情,而不是左值。这就是为什么您不能使用它的地址。


I am a beginner in programming and was trying out some combinations.

#include<stdio.h>


int main()
{
int a=5;

printf("%d",&a); // STATEMENT 1
printf("\n%d",a); //STATEMENT 2
printf("\n%d",&(a++)); //STATEMENT 3
printf("\n%d",a);  //STATEMENT 4

return 0;
}

I get a error in STATEMENT 3 saying

[Error] lvalue required as unary '&' operand

I expected the output of STATEMENT 1 & 3 to be same as both address are the same.

Also I expected the output of STATEMENT 2 to be 5 & STATEMENT 4 to be 6.

I looked up and found a similar question : Lvalue required error

I understood the issue in that question.From the comments to the first answer to above mentioned question I see lvalue as something to which something can be stored.

But I still can't understand why &(a++) or &(++a) should give an error. Any help will be appreciated.

Thank You for reading this.

[Edit] Thank you for answering. If possible please include references where the exact sequence of execution or nature of such expressions are discussed. That way rookies like me won't trouble the community with such trivial questions.

解决方案

a++ increments your a and returns the old value of a. The & operator returns the address of a variable. But the returned old value of a doesn't have an address. It's a very temporary thing, not an lvalue. That's why you can not take the address of it.

这篇关于无法理解错误条件WRT左值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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