473,375 Members | 1,299 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,375 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>
  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 7858
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

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

Similar topics

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...
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...
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...
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;
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...
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...
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
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...
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. ...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.