c - 如何在 C 结构中使用函数指针?

标签 c data-structures function-pointers

我想了解有关在 C 结构中使用函数指针作为模拟面向对象编程的方法的更多信息,但在我的搜索中,我只发现了类似 this 的问题答案是简单地使用一个函数指针,而不描述它是如何工作的。

我最好的猜测是这样的

#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 中模拟面向对象的设计通常会导致糟糕的风格。复制对结构调用的方法,以便可以使用指向结构的指针调用它们,就像我在我的示例中所做的那样,也不异常(exception)。 (坦率地说,它违反了 DRY。)结构中的函数指针对于多态性更有用。例如,如果我有一个表示元素线性序列的通用容器的结构 vector ,则存储一个 comparison_func 成员可能很有用,该成员是一个函数指针,允许对 vector 进行排序和搜索。 vector 的每个实例都可以使用不同的比较函数。但是,对于对结构本身进行操作的函数,最好有一个单独的函数,该函数不会在结构中重复。

这使得正确答案变得更加复杂。如何使我的上述示例编译正确?是如何重新格式化我上面的例子以使其具有良好的风格?还是像 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;
}

关于c - 如何在 C 结构中使用函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11481423/

相关文章:

c - c中exp函数的反函数指针

c - 如何为不同的目的/架构包含/删除 C 代码功能?

c++ - 是否有使用用户提供的提示的最近邻数据结构?

android - 表示室内地图的数据结构

c++ - 声明 const 成员函数的语法返回一个裸函数指针,没有 typedefs?

c - 测量作为参数传递的任何函数的执行时间的函数

C 字符串输入溢出其他字符串输入

c - 在 OpenCV 中获取窗口的大小

c - ANSI C - 初始化数组

集合的 Java 可修改 View