函数返回值的地址 [英] address of function's return value

查看:183
本文介绍了函数返回值的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


假设以下声明:


char * func_1(无效);

void func_2(char **);


我被允许这样做:


char * c = func_1();

func_2(& c);


但是尝试这样做:


func_s(&(func_1())) ;


给出一元无效左值?&?。


如果调用func_1()被评估为char的指针,为什么我不能直接访问其地址?


谢谢


-

Pietro Cerutti


PGP公钥:
http://gahr.ch/pgp

Hi group,

assume the following declarations:

char *func_1(void);
void func_2(char **);

I am allowed to do:

char *c = func_1();
func_2(&c);

But trying to do:

func_s(&(func_1()));

gives "invalid lvalue in unary ?&?".

If the call "func_1()" is evaluated as a pointer to char, why can''t I
access to its address directly?

Thanks

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp

推荐答案

Pietro Cerutti写道:
Pietro Cerutti wrote:

嗨组,


假设以下声明:


char * func_1 (void);

void func_2(char **);


我被允许这样做:


char * c = func_1();

func_2(& c);


但是尝试这样做:


func_s(&(func_1()));
Hi group,

assume the following declarations:

char *func_1(void);
void func_2(char **);

I am allowed to do:

char *c = func_1();
func_2(&c);

But trying to do:

func_s(&(func_1()));



错字:

func_2(&(func_2()));

Typo:
func_2(&(func_2()));


>

给出一元无效左值?&?。


如果调用func_1()被评估为char的指针,为什么我不能直接访问其地址?


谢谢
>
gives "invalid lvalue in unary ?&?".

If the call "func_1()" is evaluated as a pointer to char, why can''t I
access to its address directly?

Thanks

< br $>
-

Pietro Cerutti


PGP公钥:
http://gahr.ch/pgp


Pietro Cerutti< gahr_AT_gahr_DOT_ch_DO_NOT_SPAMsaid:


< snip>
Pietro Cerutti <gahr_AT_gahr_DOT_ch_DO_NOT_SPAMsaid:

<snip>

如果调用func_1()被评估为char的指针,为什么我不能直接访问其地址

If the call "func_1()" is evaluated as a pointer to char, why can''t I
access to its address directly?



因为它是一个右值,而不是一个左值。在获取对象的地址之前,你必须将它存储在一个对象中。




以下类比可以帮助你更清楚地理解这一点。你可以这样做:
这样做:


int i = 6;

foo(& i);


但不是这样:


foo(& 6);


HTH。手。


-

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

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

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

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

Because it''s an rvalue, not an lvalue. You have to store it into an object
before you can take the address of the object.

The following analogy may help you to understand this more clearly. You can
do this:

int i = 6;
foo(&i);

but not this:

foo(&6);

HTH. HAND.

--
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:

Pietro Cerutti< gahr_AT_gahr_DOT_ch_DO_NOT_SPAMsaid:


< snip>
Pietro Cerutti <gahr_AT_gahr_DOT_ch_DO_NOT_SPAMsaid:

<snip>

>如果调用func_1(),被评估为char的指针,为什么我不能直接访问其地址?
>If the call "func_1()" is evaluated as a pointer to char, why can''t I
access to its address directly?



因为它是一个右值,而不是一个左值。在获取对象的地址之前,你必须将它存储在一个对象中。




以下类比可以帮助你更清楚地理解这一点。你可以这样做:
这样做:


int i = 6;

foo(& i);


但不是这样:


foo(& 6);


HTH。 HAND。


Because it''s an rvalue, not an lvalue. You have to store it into an object
before you can take the address of the object.

The following analogy may help you to understand this more clearly. You can
do this:

int i = 6;
foo(&i);

but not this:

foo(&6);

HTH. HAND.



嗯......我多次听说过左撇子和左撇子,但从来没有发现过它们含义的明确说明,更不用说rvalue在

的右边是一个表达式,左值是左边的,这实际上并不是由
本身解释的。


无论如何,它很清楚..谢谢!


-

Pietro Cerutti


PGP公钥:
http://gahr.ch/ pgp


这篇关于函数返回值的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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