从另一个函数返回一个函数指针
Returning a function pointer from another function
我正在尝试编写一个函数,然后将哪个测试传递给 运行:
static uint8_t (*GetTest(uint8_t test_type))(void)
{
switch(test_type) {
case 0:
return &test_a;
case 1:
return etc etc...
}
}
static void test_a(void)
{
;
}
但是,我的编译器警告说 return 值类型与函数类型不匹配。我相信这是由于函数的静态声明,但我不确定如何包含它们。
您需要确定函数的 return 类型。
你想要这个:
#include <stdint.h>
static uint8_t test_a(void)
{
return 1; // you need to return something
}
static uint8_t(*GetTest(uint8_t test_type))(void)
{
switch (test_type) {
case 0:
return &test_a;
// case 1:
// return etc etc...
}
}
或者这个:
#include <stdint.h>
static void test_a(void)
{
}
static void(*GetTest(uint8_t test_type))(void)
{
switch (test_type) {
case 0:
return &test_a;
// case 1:
// return etc etc...
}
}
上面的两个版本编译时没有警告。
要添加到@Jabberwocky 的回答中,创建 typedef 可能更简单,即:
// this is a function with no parameters and no return value
typedef void(*TestFn)(void);
这样可以更轻松地查看 GetTest
函数 returns 的内容,以及它接受的参数:
// this is a function which accepts one uint8_t parameter,
// and returns a function with `TestFn` signature
static TestFn GetTest(uint8_t test_type)
{
switch (test_type)
{
case 0: return &test_a;
case 1: ...
}
}
看来您正在寻找这样的东西:
#include <stdio.h>
#include <stdlib.h>
typedef int (*fx_p)(int, int);
int testadd(int a, int b) { return a+b; }
int testsub(int a, int b) { return a-b; }
int testmul(int a, int b) { return a*b; }
int testdiv(int a, int b) { return a/b; }
fx_p getfx(int n) {
switch (n) {
default: return testadd;
case 4: case 5: case 6: return testsub;
case 7: case 8: return testmul;
case 9: return testdiv;
}
}
int main(void) {
// missing srand on purpose
for (int k = 0; k < 20; k++) printf("%d\n", getfx(rand() % 10)(42, 10));
return 0;
}
你可以在ideone上看到运行:https://ideone.com/U3it8W
我正在尝试编写一个函数,然后将哪个测试传递给 运行:
static uint8_t (*GetTest(uint8_t test_type))(void)
{
switch(test_type) {
case 0:
return &test_a;
case 1:
return etc etc...
}
}
static void test_a(void)
{
;
}
但是,我的编译器警告说 return 值类型与函数类型不匹配。我相信这是由于函数的静态声明,但我不确定如何包含它们。
您需要确定函数的 return 类型。
你想要这个:
#include <stdint.h>
static uint8_t test_a(void)
{
return 1; // you need to return something
}
static uint8_t(*GetTest(uint8_t test_type))(void)
{
switch (test_type) {
case 0:
return &test_a;
// case 1:
// return etc etc...
}
}
或者这个:
#include <stdint.h>
static void test_a(void)
{
}
static void(*GetTest(uint8_t test_type))(void)
{
switch (test_type) {
case 0:
return &test_a;
// case 1:
// return etc etc...
}
}
上面的两个版本编译时没有警告。
要添加到@Jabberwocky 的回答中,创建 typedef 可能更简单,即:
// this is a function with no parameters and no return value
typedef void(*TestFn)(void);
这样可以更轻松地查看 GetTest
函数 returns 的内容,以及它接受的参数:
// this is a function which accepts one uint8_t parameter,
// and returns a function with `TestFn` signature
static TestFn GetTest(uint8_t test_type)
{
switch (test_type)
{
case 0: return &test_a;
case 1: ...
}
}
看来您正在寻找这样的东西:
#include <stdio.h>
#include <stdlib.h>
typedef int (*fx_p)(int, int);
int testadd(int a, int b) { return a+b; }
int testsub(int a, int b) { return a-b; }
int testmul(int a, int b) { return a*b; }
int testdiv(int a, int b) { return a/b; }
fx_p getfx(int n) {
switch (n) {
default: return testadd;
case 4: case 5: case 6: return testsub;
case 7: case 8: return testmul;
case 9: return testdiv;
}
}
int main(void) {
// missing srand on purpose
for (int k = 0; k < 20; k++) printf("%d\n", getfx(rand() % 10)(42, 10));
return 0;
}
你可以在ideone上看到运行:https://ideone.com/U3it8W