Thread: Using function pointer within a struct

  1. #1
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46

    Question Using function pointer within a struct

    Dear,

    I have an issue with function pointer in a struct

    Consider, I have a struct

    Code:
    struct MyStruct {
    int a;
    void (*function1) ();
    }
    I have another function which use a function pointer as an argument:

    Code:
    void function2(void (*function) ());
    Now, I create an instance of a struct:
    Code:
    MyStruct* struct1;
    I also call malloc for struct1.

    Now, I want to pass function1 of struct1 to function2. How can I do it?

    I tried:

    Code:
    function2(struct1->function1);
    or
    function2(&(struct1->function1));
    or even
    function2(*(struct1->function1));
    but all of them are error and gcc reports "dereferencing pointer incomplete type".

    Please help me.
    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hannibal2010
    Now, I create an instance of a struct:
    That's just creating a pointer to MyStruct. Furthermore, unless you have an appropriate typedef it should be:
    Code:
    struct MyStruct *struct1;
    Quote Originally Posted by Hannibal2010
    I tried:
    The first one should be correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear laserlight,

    Thanks for your help.

    I modified as you describe, but the error still happen.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hannibal2010
    I modified as you describe, but the error still happen.
    Post the smallest and simplest program that demonstrates the error. (I have in my possession a small and simple program that demonstrates that there is no error.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but all of them are error and gcc reports "dereferencing pointer incomplete type".
    Meaning it doesn't know what the struct looks like.

    If the struct definition is in a .h file, you need to include that in all the files which attempt to dereference a pointer to that struct, or attempt to get the size of the structure.


    > I also call malloc for struct1.
    Which is strange really, if you managed to call malloc with the right size for the structure to begin with, then you should not be getting incomplete types later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Did you remember to assign the function pointer before trying to pass it?
    Code:
    struct1->function1 = testfn;
    Still I can't duplicate a dereferencing error exactly.

  7. #7
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46

    Exclamation

    Dear,

    This is my source code:

    Code:
    //file: test2.c
    #include "test2.h"
    
    struct Person2 {
    	Person* corePerson; //another struct, don't need to care
    	unsigned int height;
    	void (*notify_age) ();
    };
    
    
    
    
    void	person2_notify_person_age () { //focus to this function, please
    	printf ("I am person2 and I received a notification\n");
    	return;
    }
    
    
    Person2*	person2_create (unsigned int age, unsigned int height) {
    	Person2*	tempPerson2;
    	tempPerson2 = malloc (sizeof (Person2));
    	tempPerson2->corePerson = person_create (age);
    	tempPerson2->height = height;
    	printf ("I am a 2nd newcomer with age: %d and height: %d\n", person_get_age(tempPerson2->corePerson),tempPerson2->height);
    	
    	tempPerson2->notify_age = person2_notify_person_age; //assign the function pointer to real function
    	
    	return tempPerson2;
    }
    
    
    //...
    //some other functions
    And this is my test:

    Code:
    #include "test2.h"
    #include "test3.h"
    
    int main () 
    {
    	struct Person2* testPerson2;
    	struct Person2* tempPerson2;
    	int i = 0;
    
    	
    	Person* testPerson;
    	testPerson = person_create (20);
    	
    	testPerson2 = person2_create (30, 50);
    	
    	person_set_age (testPerson, 20, testPerson2->notify_age); //call function pointer, ERROR here
    		
    	
    	return 0;
    }

  8. #8
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    I defined struct Person2 in test2.c, and in test2.h, I called:
    typedef struct Person2 Person2;

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's the problem: once you access a member, you need the struct definition. A mere declaration is not enough, although it is enough to declare a pointer to the struct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear laserlight

    Can you guide me to solve this issue?

    I tried to add the get function in test2.c:

    Code:
    void*	person2_get_function_pointer (Person2* person2) {
    	return (person2->notify_age);
    }
    and then, modified the function pointer call in main.c

    Code:
    person_set_age (testPerson, 20, person2_get_function_pointer (testPerson2));
    Now, there is not error, but the function does not run. Maybe it is a NULL pointer (I guess).

  11. #11
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear laserlight

    It's done now, I fixed it.

    Thank you very very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function pointer in a struct
    By dayalsoap in forum C Programming
    Replies: 3
    Last Post: 04-02-2011, 08:57 PM
  2. pointer to struct in function
    By mad_muppet in forum C Programming
    Replies: 6
    Last Post: 06-17-2010, 02:55 PM
  3. How to pass a pointer of a struct to a function
    By drty2 in forum C Programming
    Replies: 12
    Last Post: 01-19-2009, 12:34 PM
  4. calling a function with a pointer to a struct
    By bomberto in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2006, 04:21 AM
  5. Naming pointer to function in a struct...
    By QuestionC in forum C Programming
    Replies: 4
    Last Post: 12-09-2001, 12:20 AM