简体   繁体   English

将void *指针从函数返回到其他函数参数

[英]return void* pointer from function into other function parameter

I'm, trying to build a generic list that handles Element(s) each object (pointer to struct) in the program should use this list, 我正在尝试建立一个通用列表来处理程序中每个对象(指向结构的指针)的元素,应使用此列表,

that's the definition for void*, from now on it's Element. 这就是void *的定义,从现在开始就是Element。 and the add function signature: 和添加功能签名:

typedef void* Element;
void AddElementToList(List thisList , Element toAdd);

and the create user function and the user definition: 以及创建用户功能和用户定义:

typedef struct FacebookUser_t* User;
User CreateUser(char* const lineToSplit);

that's how i call the function: 这就是我所谓的函​​数:

AddElementToList(thisServer->UsersList , (Element)CreateUser(line));

while debugging the create user function doing good and values assigned to it, just after it return it seems that the object become empty and then at the add element it crushes. 在调试create user函数时,它会做好事并为其分配值,但在返回该函数后,似乎该对象变为空,然后在add元素上击碎。 that's the return : 那就是回报:

return toAdd;

'toAdd' it's User type. “ toAdd”是用户类型。 What am I doing wrong? 我究竟做错了什么?

Difficult to say without more information, but you need to watch out for object scope: - your CreateUser function has to allocate memory for a FacebookUser_t object on the heap, using malloc or similar, returning a pointer to that (type User ; that's why @OliCharlesworth is warning about hiding pointers behind a typedef: User is not a user-object, and does not allocate memory). 无需更多信息就很难说,但是您需要注意对象范围:-您的CreateUser函数必须使用malloc或类似方法为堆上的FacebookUser_t对象分配内存,并返回指向该对象的指针(类型User ;这就是@ OliCharlesworth警告将指针隐藏在typedef后面: User不是用户对象,并且不分配内存。

Your last line suggests another possible problem. 您的最后一行提出了另一个可能的问题。 You indicated toAdd to be a parameter to the function AddElementToList , then you talk about returning 'toAdd', but the function is of type 'void'. 您指示toAdd是函数AddElementToList的参数,然后讨论返回“ toAdd”,但是该函数的类型为“ void”。 Something is confusing here IMHO, and you might want to review those sections. 恕我直言,这有些令人困惑,您可能需要查看这些部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM