From: Gruber Gerhard Newsgroups: comp.os.msdos.djgpp,comp.lang.c Subject: Re: Functions in struct's... possible? How? Date: Mon, 25 Aug 1997 11:25:29 +0200 Organization: APAnet Lines: 44 Message-ID: <34014F89.4F1B@ibm.net> References: <33FCDA5C DOT 2353659F AT execulink DOT com> NNTP-Posting-Host: 192.56.14.75 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Jeff Weeks wrote: > > I'd like to have a function in a struct that I've created. I realise I > can't just stick a functions in there, so I used a pointer to a > function. Here's what my struct looks like: > > typedef struct { > VesaInfo card; // information about the video card > VesaModeInfo mode; // information about the current mode > int bytes_per_line; // the number of bytes per scanline > int size; // the size in bytes of the whole screen > char *address; // the address of actual video memory > void (*blit)(char *); // a routine to copy a virtual screen to > memory > } Driver; > > And then I define an instance and set blit to something: > > Driver driver; > driver.blit = lfb_blit; > > But when I try to call the actual function, it crashes. I know the > lfb_blit() function is not at fault (I've tested it separately), so the > problem is in calling the function from the struct. I do it as follows: > > char *virt = (char *)malloc(640*480*2); > driver.blit(virt); > > Can C not do this? Is there no way to call a function from a struct? > If there is a way (perferable a portable way!) could you please tell me. > > If it's not possible, then I'll just write an assembly language > proceedure to load in the address of blit and jump to it, but I'd perfer > to be able to do it simply with C code. This should work ok. I use it myself in a parser where I defined for each keyword a function that processes the parameters. Are you sure that your function lfb_blit() works correct? Also you might check the models (depending on your system). Which memory model do you use? -- Bye, Gerhard