| | |
struct and member function pointer
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Solved Threads: 0
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.
Thanks
Rass
C Syntax (Toggle Plain Text)
#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
•
•
•
•
Originally Posted by Rass Saee
The following example crashes when I call the member that is pointing to a function.
#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
![]() |
Similar Threads
- putting the address of a function pointer into a struct (C)
- How to pass a function pointer as an argument? (C++)
- Is ifs is a member function of ifstream class (C++)
- Assign content to a function pointer (C)
Other Threads in the C Forum
- Previous Thread: Gauss Elimination problem
- Next Thread: Displaying a JPEG image using Windows GUI
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






