为什么我不能获取返回值的地址?
我已将一些代码分成两个文件,它之前可以工作。在一个文件中,我有一个函数,它有一个输出值参数,它是一个指向指针的指针。
我通过调用 getter 来填充此参数并取消引用它:
foo(&bar());
但是我收到一条错误消息“&”需要一个左值'。我理解这个错误的含义,但我认为我能够做到这一点,因为它是一个指针,因此代表了原始的“事物”。
这是否与实际指针内存位置可能改变的事实有关,即使它指向正确的东西?但为什么以前在同一个文件中时它可以工作,而现在却不行呢?
提前致谢!
I've split some code into two files, it was working before. In one file I have a function which has an out value parameter which is a pointer to a pointer.
I'm filling this parameter with a call to a getter and dereferencing it:
foo(&bar());
however I get an error saying ''&' requires an l-value'. I understand what this error means, but I thought I would be able to do this because it's a pointer and so does represent the original 'thing'.
Is it something to do with the fact that the actual pointers memory location could change, even though it would point to the right thing? but why did it work before when it was in the same file and not now?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,你需要一个左值,即可以分配的东西。您不能分配给函数的返回值。
你需要做这样的事情:
Well, you need an lvalue, i.e. something that can be assigned to. You can't assign to the return value of a function.
You need to do something like this:
使用 &,您不是取消引用,而是引用。要取消引用,请使用 * 代替:
foo(*(bar()));
。如果您确实想引用它,请将
bar()
的结果放入临时变量中:Using &, you aren't dereferencing, but referencing. To derefrence, use * instead:
foo(*(bar()));
.And if you do want to reference it, put the result of
bar()
in a temporary variable:假设 foo 接受一个指向 int 的指针,尝试:
Assuming
foo
takes a pointer-to-int, try:我猜
bar()
是:它按值返回指针
member_pointer
,因此不是 l-vlaue。如果你想修改
foo()
中的member_pointer
值,你需要:I guess
bar()
is:which returns the pointer
member_pointer
by value, hence not an l-vlaue.If you want to modify the value
member_pointer
infoo()
, you need: