任何人都可以告诉我原因 [英] Can anybody tell me the reason
问题描述
任何人都可以告诉我以下代码无效的原因吗?
int * funct()
{
int p = 10 ;
返回& p;
}
int * ptr;
funct()= ptr; // ------->给编译时间错误
(LValue)
任何人都可以告诉我原因这个。
Ashu写道:
int * funct( )
{
int p = 10;
return& p;
}
首先不要返回局部变量的地址。这不是编译
错误,但效果未定义,可能导致
异常。编译器会在此声明中发出警告。
int * ptr;
funct()= ptr; // ------->给编译时间错误
(LValue)
任何人都可以告诉我这个的原因。
它应该是这样的..
ptr = funct();
我们应该为左侧变量赋值(即使一般
math)。
- Murali Krishna。
" Ashu" < as *********** @ gmail.comwrote in message
news:11 ******************* ***@j8g2000cwa.googlegro ups.com ...
有人可以告诉我以下代码无效的原因吗?
int * funct()
{
int p = 10;
return& p;
}
int * ptr;
funct()= ptr; // ------->给编译时错误
func()是一个rhv(右手值)。
ptr是一个lhv(左手值/变量)。
在C和C ++中你把左边想要改变的东西放在左边(
左手值) )。
ptr = func();
表示,将func()加载到ptr中。
func()= ptr;
无效,因为你无法加载带有值的函数,你只能将
传递给它们值(好吧,在某些情况下)你可以用c ++,但这是一个不同的
故事。)
(LValue)
任何人都可以告诉我这个的原因。
Ashu写道:
有人可以告诉我以下代码无效的原因吗?
int * funct()
{
int p = 10;
return& p;
}
int * ptr;
funct()= ptr; // ------->给编译时间错误
(LValue)
任何人都可以告诉我原因这个。
看起来你甚至不打算阅读最基本的C ++书籍
!!!!
can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)
can anybody plz tell me the reason of this.
Ashu wrote:int *funct()
{
int p=10;
return &p;
}First do not return address of local variable. This is not a compile
error but the effect is undefined and it may lead to
exception. Compiler gives a warning on this statement.
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)
can anybody plz tell me the reason of this.it should be like this..
ptr = funct();
we should assign a value to left hand side variable (even in general
math).
-- Murali Krishna.
"Ashu" <as***********@gmail.comwrote in message
news:11**********************@j8g2000cwa.googlegro ups.com...can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time errorfunc() is a rhv (Right hand value).
ptr is a lhv (Left hand value/variable).
In C and C++ you put on the left the thing you want to get changed (the
left hand value).
ptr = func();
means, Load into ptr the value of func().
func() = ptr;
does not work because you can not load a function with a value, you can only
pass them values (well, in some cases you can in c++, but that''s a different
story).
(LValue)
can anybody plz tell me the reason of this.
Ashu wrote:can anybody tell me the reason why following code is not working?
int *funct()
{
int p=10;
return &p;
}
int *ptr;
funct() = ptr; //------->Giving Compile time error
(LValue)
can anybody plz tell me the reason of this.Looks like you haven''t bothered to read even the most basic C++ book
!!!!
这篇关于任何人都可以告诉我原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!