Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function pointer within a struct? 1

Status
Not open for further replies.

Flappy

Programmer
Jul 30, 2002
35
AU
how would i go about having a function pointer within a struct?

i've gotten this far and it seems to work but the function doesnt seem to be getting called:


struct Page {
char* title;
...
void (*out)(const char*);
}

void out(const char* str) {
printf("%s\n",str);
}

struct Page* pg;
pg->out=out;


pg->out("Hello");
 
sorry i left this out after the "struct Page* pg" declaration:

pg=(struct Page*)malloc(sizeof(struct Page));

 
Try this example and see if you can pick out any
mistakes in what you are doing. I don't see any quickly.

Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct {
float *attr;
char name[20];
float *(*fptr)(void);
} MOON;


float *retArr(void);

int main(void) {
MOON *xmoon = malloc(sizeof(MOON));
xmoon->fptr = retArr;

     printf(&quot;Name of moon: &quot;);
           fgets(xmoon->name,20,stdin);
           xmoon->attr = xmoon->fptr();
           printf(&quot;MOON: %s..Size: %f, Magnitude %f, Brightness %f\n&quot;,xmoon->name,xmoon->attr[0],
           xmoon->attr[1],xmoon->attr[2]);
            
return 0;
}


float *retArr(void) {
int i = 0;
float *array = malloc(3 * sizeof(float));
   
   if (array) {
       while (i < 3) {
             array[i] = (float)(1 + rand() % 15);
             i++;
       }
       return array;
   }

return NULL;
}
 
struct Page {
char* title;
...
void (*out)(const char*);
};

void out(const char* str) {
printf(&quot;%s\n&quot;,str);
}

struct Page* pg;

//you have forgotten this line (i guess)
pg=(struct Page*)malloc(sizeof(struct Page));

pg->out=out;


pg->out(&quot;Hello&quot;);
bluenote@clorinda.dnsq.org
(excuse my english)
 
its ok guys ive managed to get it to work - the actual piece of code that im programming is alot larger and was too big to paste into here....

but while on the subject is it possible for the &quot;out&quot; function to know what struct variable is calling it? as in like OOP

i know that i could just do this:

pg->out(pg,&quot;Hello&quot;);

and use the first argument to pass the pointer of the structure but is it possible to know the calling variable or somehow read back or forward within the struct to access other struct members without having to pass the pg variable to the function?

sorry if im getting confusing :/

 
thanks, thats excatly what im after - i normally program in c++ but the current server im working on at the moment only has gcc
 
You should have the g++ compiler included with your
gcc installation.
If you only had cc that would be another thing
altogether..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top