user2422190
user2422190

Reputation: 59

How do I use function pointers?

I have a problem with a function on a binary tree.The Tree houses client structs which among other thing, have an id number and a date field. I need to make 3 functions, 2 find_client functions , one searches using the node's id number, and one using the date, they both return an address to a new tree containing all matching node's in order. Above these 2 functions a find_client function to decide which function to call based on user input, I am trying to make it work using function pointers but I am running across a problem. First off, the structs:

typedef struct date{
int day;
int month;
int year;
}date;

typedef struct time{
    int hours;
    int minutes;
}time;

typedef struct client{
    char *first_name;
    char *sur_name;
    unsigned int id;
    unsigned long reg_num;
    date rent_date;
    time rent_time;
    int price_for_day;
}client;

typedef struct client_tree {
    struct client c;
    struct client_tree *left, *right;
} clientTree;
typedef union clientData{
    unsigned int id;
    date date;
}clientData;

Now the functions I'm using:

clientTree* findClient(clientTree* t){
    clientTree* n=NULL;
    int i=0;
    clientData u;
    while(i!=1 && i!=2){
        printf("\nPlease enter 1 to search via I.D. number, or press 2 to search by date: ");
        scanf("%d", &i);
        __fpurge(stdin);
        if(i==1){
            printf("\nEnter the id number please: ");
            scanf("%u", &u.id);
            __fpurge(stdin);
        }
        else if (i==2){
            printf("\nEnter the day please: ");
            scanf("%d", &u.date.day);
            __fpurge(stdin);
            printf("\nEnter the month please: ");
            scanf("%d", &u.date.month);
            __fpurge(stdin);
            printf("\nEnter the year please: ");
            scanf("%d", &u.date.year);
            __fpurge(stdin);
        }
        else
            printf("\nNot valid, try again.");
    }
    clientTree* (*pt2Function)(clientTree*, clientData) = NULL;
    pt2Function=GetPtr2(i);
    n= (*pt2Function)(t, u);
    return n;
}


clientTree* findClientDate(clientTree* t, clientData u){
    if(!t)
        return NULL;

    if(t->c.rent_date.day==u.date.day && t->c.rent_date.month==u.date.month && t->c.rent_date.year==u.date.year){
        clientTree* n = createClientTree();
        treeAddClient(n,t->c);
        n->left=findClientDate(t->left, u);
        n->right=findClientDate(t->right, u);
        return n;
    }
    return NULL;
}
clientTree* findClientId(clientTree* t, clientData u){
    if(!t)
        return NULL;

    if(t->c.id==u.id){
        clientTree *n = createClientTree();
        treeAddClient(n,t->c);
        n->left=findClientId(t->left, u);
        n->right=findClientId(t->right, u);
        return n;
    }
    return NULL;
}


clientTree*(*GetPtr2(int opCode))(clientTree*, clientData){
if(opCode == 1)
    return &findClientId;
else
    return &findClientDate;
}

I'm getting an error: "conflicting types for ‘GetPtr2’" I'm not to handy with function pointers, any suggestions?

P.S. also these 2 functions are called on:

clientTree* treeAddClient(clientTree* root, client c){
    if (!root) {
        root=createClientTree();
        root->c=c;
        return root;
    }
    if (c.id > root->c.id)
        root->right = treeAddClient(root->right, c);
    else if (c.id < root->c.id)
        root->left = treeAddClient(root->left, c);
    else
        return NULL;
    return root;
}

clientTree* createClientTree(){
    clientTree *t;
    t=ALLOC(clientTree, 1);
    return t;
}

Upvotes: 1

Views: 176

Answers (2)

Taylor Brandstetter
Taylor Brandstetter

Reputation: 3623

Your declaration of GetPtr2 looks correct... But where is it first being declared? That's probably the source of the conflict reported by the compiler.

Also, consider using a typedef to make things simpler:

typedef clientTree *(* MyFuncPtr)(clientTree *, clientData);

MyFuncPtr GetPtr2(int opCode);

Upvotes: 1

Mahesh
Mahesh

Reputation: 34625

clientTree* (*pt2Function)(clientTree*, clientData) = NULL;

Here you intialized pt2Function to NULL. And ptrFunction is a function pointer that can point to a function which can take 2 parameters of type clientTree*, clientData and whose return type is clientTree*.

So, in your example you can use like -

pt2Function = findClientDate;

Now you can call the function findClientDate through pt2Function like -

(*findClientDate)(t,u);

So, in your example you should change the signature of the function clientTree*(*GetPtr2(int opCode))(clientTree*, clientData). It should be -

clientTree* GetPtr2(int opCode);

And now you can declare a function pointer like -

clientTree* (*fPtr)(int opCode) = NULL;
fPtr = GetPtr2;

Upvotes: 2

Related Questions