簡體   English   中英

如何在C結構中使用函數指針?

[英]How to use a function pointer in a C struct?

我想了解更多關於使用C中的結構函數指針的方式來模仿面向對象編程,但在我的搜索,我找到這樣的問題這個地方簡單的答案是,將如何使用函數指針沒有描述工作。

我最好的猜測是這樣的

#include <stdio.h>
#include <stdlib.h>

struct my_struct
{
    int data;
    struct my_struct* (*set_data) (int);
};

struct my_struct* my_struct_set_data(struct my_struct* m, int new_data)
{
    m->data = new_data;
    return m;
}

struct my_struct* my_struct_create() {
    struct my_struct* result = malloc((sizeof(struct my_struct)));
    result->data = 0;
    result->set_data = my_struct_set_data;
    return result;
}

int main(int argc, const char* argv[])
{
    struct my_struct* thing = my_struct_create();
    thing->set_data(1);
    printf("%d\n", thing->data);
    free(thing);
    return 0;
}

但這給了我編譯器警告warning: assignment from incompatible pointer type ,所以很明顯我做錯了。 有人可以提供一個小但完整的例子,說明如何正確使用C結構中的函數指針?

我用C語言教的課程甚至沒有提到這些。 這讓我想知道這些是否真的被C程序員使用。 在C結構中使用函數指針有哪些優缺點?

Andy Stow Away給出的答案修復了我的編譯器警告,但沒有回答我的第二個問題。 eddieantonio和Niklas R給出的答案的評論回答了我的第二個問題,但是沒有修復我的編譯器警告。 所以我把它們匯集成一個答案。

C不是面向對象的,並且試圖在C中模擬面向對象的設計通常會導致糟糕的風格。 復制方法調用結構,以便可以使用指向結構的指針調用它們,就像我在我的示例中所做的那樣也不例外。 (坦率地說,它違反了DRY。)結構中的函數指針對於多態性更有用。 例如,如果我有一個結構向量來表示線性元素序列的通用容器,那么存儲一個compare_func成員可能是有用的,該成員是一個函數指針,允許對向量進行排序和搜索。 向量的每個實例都可以使用不同的比較函數。 但是,對於在結構本身上運行的函數的情況,具有在結構中不重復的單個單獨函數是更好的樣式。

這使得正確答案更加復雜。 如何使我上面的例子編譯是正確的? 它是如何重新格式化我的上面的例子,以便它具有良好的風格? 或者它是一個使用函數指針的結構的例子,就像C程序員那樣做? 在提出我的問題時,我沒想到答案是我的問題是錯的。 為了完整起見,我將提供問題的每個答案的示例。

修復編譯器警告

#include <stdio.h>
#include <stdlib.h>

struct my_struct
{
    int data;
    struct my_struct* (*set_data) (struct my_struct*, int);
};

struct my_struct* my_struct_set_data(struct my_struct* m, int new_data)
{
    m->data = new_data;
    return m;
}

struct my_struct* my_struct_create()
{
    struct my_struct* result = malloc((sizeof(struct my_struct)));
    result->data = 0;
    result->set_data = my_struct_set_data;
    return result;
}

int main(int argc, const char* argv[])
{
    struct my_struct* thing = my_struct_create();
    thing->set_data(thing, 1);
    printf("%d\n", thing->data);
    free(thing);
    return 0;
}

重新格式化樣式

#include <stdio.h>
#include <stdlib.h>

struct my_struct
{
    int data;
};

void my_struct_set_data(struct my_struct* m, int new_data)
{
    m->data = new_data;
}

struct my_struct* my_struct_create()
{
    struct my_struct* result = malloc((sizeof(struct my_struct)));
    result->data = 0;
    return result;
}

int main(int argc, const char* argv[])
{
    struct my_struct* thing = my_struct_create();
    my_struct_set_data(thing, 1);
    printf("%d\n", thing->data);
    free(thing);
    return 0;
}

演示在結構中使用函數指針

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct my_struct
{
    void* data;
    int (*compare_func)(const void*, const void*);
};

int my_struct_compare_to_data(struct my_struct* m, const void* comparable)
{
    return m->compare_func(m->data, comparable);
}

struct my_struct* my_struct_create(void* initial_data,
        int (*compare_func)(const void*, const void*))
{
    struct my_struct* result = malloc((sizeof(struct my_struct)));
    result->data = initial_data;
    result->compare_func = compare_func;
    return result;
}

int int_compare(const void* a_pointer, const void* b_pointer)
{
    return *(int*)a_pointer - *(int*) b_pointer;
}

int string_compare(const void* a_pointer, const void* b_pointer)
{
    return strcmp(*(char**)a_pointer, *(char**)b_pointer);
}

int main(int argc, const char* argv[])
{
    int int_data = 42;
    struct my_struct* int_comparator =
            my_struct_create(&int_data, int_compare);

    char* string_data = "Hello world";
    struct my_struct* string_comparator =
             my_struct_create(&string_data, string_compare);

    int int_comparable = 42;
    if (my_struct_compare_to_data(int_comparator, &int_comparable) == 0)
    {
        printf("The two ints are equal.\n");
    }

    char* string_comparable = "Goodbye world";
    if (my_struct_compare_to_data(string_comparator,
            &string_comparable) > 0)
    {
        printf("The first string comes after the second.\n");
    }

    free(int_comparator);
    free(string_comparator);

    return 0;
}

在結構定義中,將其更改為

struct my_struct
{
    int data;
    struct my_struct* (*set_data) (struct my_struct*,int);
};

現在使用main中的上述函數指針作為

thing->set_data(thing,1);

暫無
暫無

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

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