470,858 Members | 1,727 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,858 developers. It's quick & easy.

Initializing a structure with a function pointer

As the title says, I'm trying to put a function pointer in a structure at initialization time. The code below demonstrates this and also includes code for testing whether or not it works. It's fairly self-explanatory with the comments.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  4. // The structure containing the function pointer
  5. typedef struct {
  6.     int (*MyFunctionPointer)(int);
  7. } MyStructure;
  9. // A function to take a function pointer, initialize it with a MyStructure, and return it
  10. MyStructure *put_function_pointer_into_structure(int (*FunctionPointer)(int))
  11. {
  12.     MyStructure *StructureInstance = {FunctionPointer};
  13.     return StructureInstance;
  14. }
  16. // A trivial function for testing purposes
  17. int divide_by_two(int number)
  18. {
  19.     return number/2;
  20. }
  22. // Entry-point. Performs the testing
  23. int main(int argc, char *argv[])
  24. {
  25.     MyStructure *StructureWithFunctionPointer = put_function_pointer_into_structure(divide_by_two);
  27.     printf("The number is %i, half of it is %i\n", 9000,
  28.            StructureWithFunctionPointer->MyFunctionPointer(9000));
  29.     return EXIT_SUCCESS;
  30. }
When I try to compile this I get this warning:

func_ptr_test.c:12: warning: initialization from incompatible pointer type
(This refers to the line MyStructure *StructureInstance = {FunctionPointer};)

Of course that is just a warning and I can run the program regardless, but it gives a segfault immediately (or a "general protection error" for you Windows people out there).

I can't for the life of me figure out what is wrong with my code, any suggestions will be highly appreciated.

I'm using GCC (version 4.1.3).
Feb 3 '08 #1
2 7189
9,208 Expert Mod 8TB
You have several errors:
1) you have not created a MyStructure variable so there is no member to assign to. All you did was create a pointer to a MyStrucure.
2) The name of a function is the address of the function. Therefore, the function argument is used without parentheses. It's a variable. Not a function.

The code below compiles and links. You will need to free() the StructureInstance yourself.

Expand|Select|Wrap|Line Numbers
  1. // A function to take a function pointer, initialize it with a MyStructure, and return it
  2. MyStructure *put_function_pointer_into_structure(int (*FunctionPointer)(int))
  3. {
  4.     MyStructure* StructureInstance =
  5.     (MyStructure*)malloc(sizeof(MyStructure));
  6.     StructureInstance->MyFunctionPointer = FunctionPointer;
  7.     return StructureInstance;
  8. }
Feb 3 '08 #2
Hey thanks man... What was I thinking :)
Feb 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Doug C via .NET 247 | last post: by
10 posts views Thread by Bart Goeman | last post: by
7 posts views Thread by Jake Thompson | last post: by
7 posts views Thread by Paminu | last post: by
5 posts views Thread by hankypan1 | last post: by
13 posts views Thread by WaterWalk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.