Sam12
Sam12

Reputation: 1805

Pointers to functions casting

I have the following structs:

struct mtmFlix_t {
    List usersList;
    List seriesList;
};

struct User_t {
    int age;
    char* username;
    MtmFlix mtmFlix;
};

These are the typedefs in list.h :

typedef void* ListElement;
typedef ListElement(*CopyListElement)(ListElement);
typedef void(*FreeListElement)(ListElement);

These are the typedefs in user.h and MtmFlix.h :

typedef struct User_t *User;
typedef struct mtmFlix_t* MtmFlix;

I would like to use the following function in mtmflixCreate, but I can't seem to figure out how to cast the UserCreate and UserFree to (*void) ?

List listCreate(CopyListElement copyElement, FreeListElement freeElement);

MtmFlix mtmFlixCreate()
{
    MtmFlix newMtmFlix = malloc(sizeof(*newMtmFlix));
    if (newMtmFlix == NULL) {
        return NULL;
    }
    newMtmFlix->seriesList=listCreate(?????);
    newMtmFlix->usersList=listCreate(?????);
}

The following functions appear in user.h :

User UserCreate(MtmFlix mtmFlix, const char* username,int age);
Void UserFree(User user);

Upvotes: 0

Views: 63

Answers (1)

You don't. You have to create functions that have the needed types. Something like this:

ListElement CopyUserListElement(ListElement elem) {
    // (ListElement) is not necessary here, but included for completeness
    return (ListElement)CopyUser((User_t*)elem);
}

void FreeUserListElement(ListElement elem) {
    UserFree((User_t*)elem);
}

Upvotes: 1

Related Questions