簡體   English   中英

如何使用函數指針?

[英]How do I use function pointers?

我對二叉樹上的函數有問題。樹上包含客戶端結構,其中包括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'的類型沖突”我不方便使用函數指針,有什么建議嗎?

PS還調用了這兩個功能:

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* (*pt2Function)(clientTree*, clientData) = NULL;

在這里,您將pt2Function初始化為NULL。 ptrFunction是一個函數指針,可以指向一個函數,該函數可以采用2個類型的參數clientTree*, clientData和返回類型為clientTree*

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

pt2Function = findClientDate;

現在,您可以通過pt2Function調用函數findClientDate ,例如-

(*findClientDate)(t,u);

因此,在您的示例中,您應該更改函數clientTree*(*GetPtr2(int opCode))(clientTree*, clientData) 它應該是 -

clientTree* GetPtr2(int opCode);

現在,您可以聲明一個函數指針,例如-

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

您對GetPtr2的聲明看起來是正確的...但是首先在哪里聲明呢? 這可能是編譯器報告的沖突根源。

另外,考慮使用typedef使事情更簡單:

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

MyFuncPtr GetPtr2(int opCode);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM