1

I'm trying to write a function which will then pass on which test to run:

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)
{
   ;
}

However, I get a warning from my compiler saying that the return value type does not match the function type. I believe that this is due to the static declarations of the function, but I am unsure of how to include them.

5
  • 3
    GetTest takes a parameter of unit8_t. test_a has no parameters. They are not compatible. Commented Jul 22, 2019 at 8:40
  • Yes, I eliminated that from the code, but normally the uint8_t test_type will choose the test to run based on a switch case statement Commented Jul 22, 2019 at 8:41
  • 1
    What did you eliminate from the code? void test_a(void) is not the same as uint8_t test(void). Commented Jul 22, 2019 at 8:45
  • Ahhh so for my example it should be uint8_t (*GetTest(void))(uint8_t test_type) ? Edit: No that doesn't make sense or work Commented Jul 22, 2019 at 8:45
  • 1
    something like this? Commented Jul 22, 2019 at 8:52

3 Answers 3

4

You need to make up your mind about the return type of your function.

You want either this:

#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...
  }
}

or this:

#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...
  }
}

Both versions above compile without warnings.

Sign up to request clarification or add additional context in comments.

Comments

4

To add to @Jabberwocky's answer, it would probably be simpler for you to create a typedef, i.e.:

// this is a function with no parameters and no return value
typedef void(*TestFn)(void);

so that it's easier to see what the GetTest function returns, and what it accepts as parameters:

// 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: ...
    }
}

Comments

2

It appears you are looking for something like this:

#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;
}

You can see it running on ideone: https://ideone.com/U3it8W

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.