| delorie.com/archives/browse.cgi | search |
| From: | "A. Sinan Unur" <asu1 AT cornell DOT edu> |
| Newsgroups: | comp.os.msdos.djgpp |
| Subject: | Re: Functions in struct's... possible? How? |
| Date: | Thu, 21 Aug 1997 21:03:43 -0400 |
| Organization: | Cornell University http://www.cornell.edu |
| Lines: | 85 |
| Sender: | asu1 AT cornell DOT edu (Verified) |
| Message-ID: | <33FCE56F.5F52@cornell.edu> |
| References: | <33FCDA5C DOT 2353659F AT execulink DOT com> |
| Reply-To: | asu1 AT cornell DOT edu |
| NNTP-Posting-Host: | cu-dialup-0063.cit.cornell.edu |
| Mime-Version: | 1.0 |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
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?
it would be good to see what you actually compiled along with the any
warning messages you received (assuming you compiled with -Wall.) the
following code compiles and runs fine:
#include <stdlib.h>
#include <stdio.h>
/* following are just so that i could compile this */
#define VesaInfo int
#define VesaModeInfo int
typedef struct {
VesaInfo card;
VesaModeInfo mode;
int bytes_per_line;
int size;
char *address;
void (*blit)(char *);
} Driver;
void lfb_blit(char *c)
{
*c = (char) 'a';
return;
}
int main(void)
{
Driver driver;
char* virt = malloc(640*480*2);
if ( NULL == virt)
{
printf("Cannot allocate memory.\n");
exit(1);
}
driver.blit = lfb_blit;
driver.blit(virt);
return 0;
}
--
Sinan
*******************************************************************
A. Sinan Unur WWWWWW
|--O+O
mailto:sinan DOT unur AT cornell DOT edu C ^
http://www.people.cornell.edu/pages/asu1/ \ ~/
Unsolicited e-mail is _not_ welcome, and will be billed for.
*******************************************************************
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |