473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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>
  3.  
  4. // The structure containing the function pointer
  5. typedef struct {
  6.     int (*MyFunctionPointer)(int);
  7. } MyStructure;
  8.  
  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. }
  15.  
  16. // A trivial function for testing purposes
  17. int divide_by_two(int number)
  18. {
  19.     return number/2;
  20. }
  21.  
  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);
  26.  
  27.     printf("The number is %i, half of it is %i\n", 9000,
  28.            StructureWithFunctionPointer->MyFunctionPointer(9000));
  29.     return EXIT_SUCCESS;
  30. }
  31.  
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 7862
weaknessforcats
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. }
  9.  
Feb 3 '08 #2
Hey thanks man... What was I thinking :)
Feb 3 '08 #3

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

Similar topics

1
by: Doug C via .NET 247 | last post by:
Using C#... I am pulling shared memory in to my app that is in the form of apredefined structure. I have arrays in 2 sub-structures. Onearray is an array of another predefined structure, and the...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
5
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non -cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
2
by: Martin Payne | last post by:
I am trying to initialise a structure with random values for all its fields. It is a large structure so I do not want to do it for each element in turn (it would take ages). Please note my...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.