struct and member function pointer

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 1
Reputation: Rass Saee is an unknown quantity at this point 
Solved Threads: 0
Rass Saee Rass Saee is offline Offline
Newbie Poster

struct and member function pointer

 
0
  #1
Apr 26th, 2006
Does anybody know how member of function can be a pointer to a function. The following example crashes when I call the member that is pointing to a function.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. typedef long fooPtr(char *);
  6. typedef struct
  7. {
  8. fooPtr *func;
  9. long cnt;
  10. } MyStruct;
  11. /******************************************************************************/
  12. long func(char * str)
  13. {
  14. printf("%s\n", str);
  15. return -1;
  16. }
  17. /******************************************************************************/
  18. void main(int argc, char *argv[])
  19. {
  20. MyStruct *ex;
  21. long l = 0;
  22.  
  23. ex = (MyStruct *)malloc(sizeof(MyStruct));
  24. if ( ex == NULL )
  25. printf("malloc failed");
  26. ex->cnt = 99;
  27. l = ex->func("Hello"); // crashes here
  28.  
  29. free(ex);
  30. return;
  31. }

Thanks
Rass
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: struct and member function pointer

 
1
  #2
Apr 26th, 2006
Originally Posted by Rass Saee
The following example crashes when I call the member that is pointing to a function.
You don't call a member function because you have not yet assigned one.
#include <stdio.h>
#include <stdlib.h>

typedef long fooPtr(char *);
typedef struct
{
   fooPtr *func;
   long cnt;
} MyStruct;
/******************************************************************************/
long func(char * str)
{
   printf("%s\n", str);
   return -1;
}
/******************************************************************************/
int main(void)
{
   long l = 0;
   MyStruct *ex = malloc(sizeof *ex);
   if ( ex != NULL )
   {
      ex->func = func;
      ex->cnt = 99;
      l = ex->func("Hello"); // crashes here
      free(ex);
   }
   return 0;
}
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC