【问题标题】:void * function pointervoid * 函数指针
【发布时间】:2013-11-14 02:03:04
【问题描述】:

我已经为一个函数指针创建了一个 typedef,它接受一个整数并返回一个 void *:

typedef void* (*fp)(int index);

然后我创建了一个结构,其中包含一个 fp 和另一个相同类型的结构:

typedef struct fp_holder {
    fp function_pointer;
    iterable *next;
} fp_holder;

我想弄清楚如何在 fp_holder 中调用 fp。

为了测试这一点,我做了以下操作:

void *test_fp(int index) {
    if (index == 0) {
        printf('H');
        fflush(stdout);
        return [something_that_works];
    }
    else if (index == 1) {
        printf('e');
        fflush(stdout);
        return [something_that_works];
    }
    else if (index == 2) {
        printf('l');
        fflush(stdout);
        return [something_that_works];
    }
    else if (index == 3) {
        printf('l');
        fflush(stdout);
        return [something_that_works];
    }
    else if (index == 4) {
        printf('o');
        fflush(stdout);
        return [something_that_works];
    }
    else {
        return (void *) NULL;
    }
}

fp_holder *a = (fp_holder *) malloc(sizeof(fp_holder));
a->function_pointer = test_fp;
a->next = NULL;

因此,通过所有这些设置,我尝试通过以下操作来调用 a 的 function_pointer:

a->function_pointer(0);
(*a->function_pointer)(0);
((*)a->function_pointer)(0);

我只是无法弄清楚为什么这些不起作用。 帮助将不胜感激! :)

编辑

我正在尝试做的事情: 使用参数调用 a 的 function_pointer。

我现在会尝试一些答案,看看会发生什么。

EDIT2

回答!我通过执行 a->function_pointer(0) 来正确调用它,但是给我一个分段错误 [这是我的问题 - 也许我应该澄清这一点] 是 printf 语句而不是我的调用。 printf 需要一个字符串,而不是我输入的字符。

【问题讨论】:

  • 你试过(a->function_pointer)(0);
  • @PaulDraper 与a->function_pointer(0); 相同。但实际上,它(两者)都应该起作用。 Proof.
  • 您遇到什么错误? (如果有的话?)
  • 天啊...只要问,我会添加它...无需写“-1让我们猜测”来试图激怒其他人。 “我试图弄清楚如何在 fp_holder 中调用 fp。”基本上,我正在尝试使用参数 0 调用 a 的 function_pointer。
  • @Supervisor 那么也许以一种我们不需要猜测的方式描述你的问题......

标签: c function pointers


【解决方案1】:

你的原始代码真的可以吗

printf('H');

而不是

printf("H");

?

您发布的代码的简化版本,带有正确的 printf 参数:

#include <stdio.h>

typedef void* (*function_pointer_t)(int index);

struct function_holder {
    function_pointer_t callback;
};

void* testFn(int i)
{
    printf("testFn %d\n", i);
}

int main(void) {
    struct function_holder fh = { testFn };
    struct function_holder* fhp = &fh;

    fh.callback = testFn;
    fh.callback(1);
    fhp->callback(2);

    return 0;
}

按预期工作:http://ideone.com/1syLlG

【讨论】:

  • 这与原始问题有什么关系?这应该是评论,而不是答案。
  • 由于他调用函数指针的尝试应该成功,因此该错误可能正在破坏内存并导致其失败。
  • @Barmar 可能,但这仍然不能回答问题。无论如何,我们不知道错误是什么,因为 OP 未能描述它。
  • @H2CO3 这是一个长期的猜测,因为当我写我的答案的第一个版本时,他的帖子没有包含对问题的准确描述。
  • @kfsone 不幸的是,它仍然不包含任何内容。
【解决方案2】:

您的代码有几个错误。我不确定您要通过此代码实现什么,但这是您的工作代码。

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

typedef void* (*fp)(int index);
typedef struct fp_holder {
    fp function_pointer;
    struct fp_holder *next;
} fp_holder;

void *test_fp(int index) {
    if (index == 0) {
        printf("H");
        fflush(stdout);
        return "H";
    }
    else if (index == 1) {
        printf("e");
        fflush(stdout);
        return "e";
    }
    else if (index == 2) {
        printf("l");
        fflush(stdout);
        return "l";
    }
    else if (index == 3) {
        printf("l");
        fflush(stdout);
        return "l";
    }
    else if (index == 4) {
        printf("o");
        fflush(stdout);
        return "o";
    }
    else {
        return (void *) NULL;                                                                                                                                    }
    }


int main() {
    fp_holder *a = (fp_holder *) malloc(sizeof(fp_holder));
    a->function_pointer = test_fp;
    a->next = NULL;
    a->function_pointer(0);
}

【讨论】:

    猜你喜欢
    • 2016-11-23
    • 2020-06-11
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多