C 如何使用函数指针?

C 如何使用函数指针?,c,tree,binary-tree,function-pointers,C,Tree,Binary Tree,Function Pointers,我对二叉树上的函数有一个问题。这棵树包含客户端结构,其中包括一个id号和一个日期字段。我需要做3个函数,2个find_client函数,一个使用节点的id号进行搜索,一个使用日期,它们都会将一个地址返回到一个新的树中,该树按顺序包含所有匹配的节点。在这两个函数上面有一个find_client函数,用于根据用户输入决定调用哪个函数,我试图使用函数指针使其工作,但遇到了一个问题。首先,结构: typedef struct date{ int day; int month; int year; }da

我对二叉树上的函数有一个问题。这棵树包含客户端结构,其中包括一个id号和一个日期字段。我需要做3个函数,2个find_client函数,一个使用节点的id号进行搜索,一个使用日期,它们都会将一个地址返回到一个新的树中,该树按顺序包含所有匹配的节点。在这两个函数上面有一个find_client函数,用于根据用户输入决定调用哪个函数,我试图使用函数指针使其工作,但遇到了一个问题。首先,结构:

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;
现在我使用的函数是:

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;
}
我收到一个错误:“GetPtr2的类型冲突” 我对函数指针不在行,有什么建议吗

另外,这两个功能也被调用:

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;
}
clientTree*treeAddClient(clientTree*root,客户端c){
如果(!root){
root=createClientTree();
根->c=c;
返回根;
}
如果(c.id>根->c.id)
root->right=treeAddClient(root->right,c);
else if(c.idc.id)
root->left=treeAddClient(root->left,c);
其他的
返回NULL;
返回根;
}
clientTree*createClientTree(){
clientTree*t;
t=ALLOC(clientTree,1);
返回t;
}
这里您将
pt2Function
初始化为NULL。而
ptrFunction
是一个函数指针,它可以指向一个函数,该函数可以接受类型为
clientTree*、clientData
的两个参数,其返回类型为
clientTree*

因此,在您的示例中,您可以使用-

pt2Function = findClientDate;
现在您可以通过
pt2Function
调用函数
findClientDate
,如-

(*findClientDate)(t,u);
因此,在您的示例中,您应该更改函数
clientTree*(*GetPtr2(int-optcode))(clientTree*,clientData)
的签名。应该是-

clientTree* GetPtr2(int opCode);
现在您可以声明一个函数指针,如-

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

您的GetPtr2声明看起来是正确的。。。但它首先在哪里被宣布?这可能是编译器报告的冲突的根源

也可以考虑使用TyPulf使事情变得简单:

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

MyFuncPtr GetPtr2(int opCode);

今天请阅读有关函数指针的教程:。这可能会有帮助。
typedef clientTree *(* MyFuncPtr)(clientTree *, clientData);

MyFuncPtr GetPtr2(int opCode);