是否可以从嵌套函数调用返回的对象中获取指针?
作为我正在进行的 C 编程项目的一部分,我需要创建自己的动态数组系统。
由于我的数组系统在添加对象时从指针复制数据,因此我希望通过将指针传递给函数中加载的对象来降低开销,而不是静态加载对象并通过添加来复制内存将其添加到之后的数组中。
我只是不明白为什么这不起作用:
some_obj_ptr = (OBJTYPE *)array_push(obj_list_ptr, &(load_item_from_file("rsrc\\item.txt")));
我可以做类似的事情吗?我对该行得到的编译错误是
lvalue required as unary '&' operand.
我的 array_push 函数的原型是:
void *array_push(ARRAY *array, void *object);
有什么想法吗?
力杰
As part of a C programming project I'm doing, I've needed to create my own dynamic array system.
As my array system copies the data from a pointer when adding an object, I would like to keep the overhead down by passing a pointer to an object that was loaded in the function, as opposed to loading the object statically and copying the memory by adding it to the array after.
I just can't understand why this won't work:
some_obj_ptr = (OBJTYPE *)array_push(obj_list_ptr, &(load_item_from_file("rsrc\\item.txt")));
Is there something similar I could do? The compile error I get for that line is
lvalue required as unary '&' operand.
The prototype for my array_push functon is:
void *array_push(ARRAY *array, void *object);
Any ideas?
LJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数的返回值是临时的,它不是命名对象(粗略地说,这是左值的定义)。您无法获取临时对象的地址,因为它并不真正存在。
当然,您可以从函数返回指针。
The return value of a function is temporary, it's not a named object (crudely speaking, that's the definition of an
lvalue
). You can't take the address of a temporary object, because it doesn't really exist.You can, of course, return a pointer from a function.