1

I have been struggling with function pointers for some time now. Maybe you guys can help me out.

In my project I have multiple functionalities for the same device and each functionality is written in its own .c file. The generic functionalities (which apply to all device functions) are written in generic.c.

I would like to create a function pointer array in the generic c file and fill that array in the other function files. So I can call the right function based on a device function identifier in the generic file.

What I have right now:

// in generic.h:
typedef void (*func_ptr_t[])(arguments);
extern func_ptr_t devFunctions[3];

And I would like to:

// in function1.c:
#include "generic.h"

devFunctions[1] = &functionName;

But then it complains about a type specifier missing AND the array initializer must be a list. If I add the type like

func_ptr_t devFunctions[1] = &functionName;

I get an error about an incomplete element type 'func_ptr_t'.

I can't initialize the entire array list from one file since it is filled from multiple files.

Anybody got an idea on how to tackle this?

Thanks!

-edit-

Because you can't use statements outside of functions I've changed my application. Now it does not use an array anymore and it updates the function pointer in the generic.c upon calling the specific function file.

So the end result:

In generic.h:

typedef void (*func_ptr_t)(<function arguments>)

In generic.c:

func_ptr_t devFunction;

In function1.c:

#include "generic.h"
extern func_ptr_t devFunction;

void functionToBeCalledFromMain( void ){
    devFunction = functionName;
}

void functionName (void ){
    // Function to be called from generic.c via function pointer
}
3
  • 3
    You define func_ptr_t as an array (of unknown size) of pointers to functions. Probably not what you really want. Commented Jul 3, 2018 at 9:52
  • 1
    And as usual, you can't have general statements outside of functions (like your assignment), only declarations and definitions. Commented Jul 3, 2018 at 9:53
  • 1
    The type specifier missing is caused by (argument) which is not a correct arguments definition. All the functions in the array must have the same signature. Commented Jul 3, 2018 at 9:56

3 Answers 3

1

As most of you pointed out, filling the array is a statement which can't be put outside a function (duh...). So what I wanted is not really going to work in this case. I rewritten my application to update a function pointer (not an array anymore) on every run with the function I need. Saves a lot of trouble :)

Thanks guys!

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

1 Comment

Can you share the final code as an edit at the end of your post. Will help the next person who has a similar question.
0

For an example about functions of the type int f(int);, you can check :

/* function definition */
int functionName(int arg)
{
    /* do things */
    return arg * 2;
}

/* create the type "function pointer int->int" */
typedef int (*func_ptr_t)(int);

/* create the array */
func_ptr_t devFunctions[3];

int main(void)
{   
    /* associate the good function */
    devFunctions[0] = &functionName;

    /* call it with an arg */
    return devFunctions[0](42);
}

Comments

0

Putting the above comments together, you probably wanted

typedef void (*func_ptr_t)(arg_type_list);

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.