开发者

Is it possible to get a pointer from an object returned by a nested function call?

开发者 https://www.devze.com 2023-04-03 03:59 出处:网络
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 ov

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 functi开发者_运维技巧on, 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号