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.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
typedef long fooPtr(char *);
typedef struct
{
   fooPtr *func;
   long cnt;
} MyStruct;
/******************************************************************************/
long func(char * str)
{
   printf("%s\n", str);
   return -1;
}
/******************************************************************************/
void main(int argc, char *argv[])
{
   MyStruct *ex;
   long l = 0;

   ex = (MyStruct *)malloc(sizeof(MyStruct));
   if ( ex == NULL )
      printf("malloc failed");
   ex->cnt = 99;
   l = ex->func("Hello"); // crashes here

   free(ex);
   return;
}

Thanks
Rass

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;
}
This article has been dead for over six months. Start a new discussion instead.