k-man
k-man

Reputation: 1221

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. 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. that's the return :

return toAdd;

'toAdd' it's User type. What am I doing wrong?

Upvotes: 0

Views: 989

Answers (1)

Johan Bezem
Johan Bezem

Reputation: 2594

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).

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'. Something is confusing here IMHO, and you might want to review those sections.

Upvotes: 1

Related Questions