pointer to function with structure

brad at bradley.UUCP brad at bradley.UUCP
Thu Jan 31 12:27:00 AEST 1985


I can't seem to get the hang of this one.  Anyone got anyideas on
how to do it.  below is the code I thought would work but it doesn't.

Basically what I want to do is to be able to insert the function into
the structure and have it call that function.


Bradley Smith			UUCP: {cepu,ihnp4,noao,uiucdcs}!bradley!brad
Text Processing			ARPA: cepu!bradley!brad at UCLA-LOCUS
Bradley University		PH: (309) 676-7611 Ext. 446
Peoria, IL 61625
------------------------cut here------------------------
#include <stdio.h>

struct mesg {
	char *m_str;
	int (*funcp)();
};

int f1();
int f2();
struct mesg mesg[] = {
	{"This is message 1", f1 },
	{"This is message 2", f2 },
};

main()
{
	char gt[5];

	while(gets(gt) != NULL) {
		switch(gt[0]) {
			case '1':
				mesg[0].(*funcp)();
				break;
			case '2':
				mesg[1].(*funcp)();
				break;
		}
	}
}
f1()
{
	printf("%s\n", mesg[0].m_str);
	printf("f1\n");
}
f2()
{
	printf("%s\n", mesg[1].m_str);
	printf("f1\n");
}



More information about the Comp.lang.c mailing list