[net.lang.c] pointer to function with structure

brad@bradley.UUCP (01/31/85)

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@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");
}

brad@bradley.UUCP (01/31/85)

I found it here it is if you want.


#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)(1);
				break;
			case '2':
				(*mesg[1].funcp)(2);
				break;
		}
	}
}
f1(i)
int i;
{
	printf("%s\n", mesg[i-1].m_str);
	printf("%d\n",i);
}
f2(i)
int i;
{
	printf("%s\n", mesg[i-1].m_str);
	printf("%d\n",i);
}

friesen@psivax.UUCP (Stanley Friesen) (02/04/85)

In article <9300002@bradley.UUCP> brad@bradley.UUCP writes:
>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.
>
>
>------------------------cut here------------------------
>		switch(gt[0]) {
>			case '1':
>				mesg[0].(*funcp)();
>				break;
>			case '2':
>				mesg[1].(*funcp)();
>				break;
>		}

	Here's your problem, this should be:

		switch(gt[0]) {
			case '1':
				(*mesg[0].funcp)();
				break;
			case '2':
				(*mesg[1].funcp)();
				break;
		}

-- 

				Sarima (Stanley Friesen)

{trwrb|allegra|cbosgd|hplabs|ihnp4|aero!uscvax!akgua}!sdcrdcf!psivax!friesen
 or
quad1!psivax!friesen

dre@ptsfa.UUCP (Doug East) (02/06/85)

> 	while(gets(gt) != NULL) {
> 		switch(gt[0]) {
> 			case '1':
> 				(*mesg[0].funcp)(1);
> 				break;
> 			case '2':
> 				(*mesg[1].funcp)(2);
> 				break;
> 		}
> 	}
> }

Actually, this works quite well also:
	.
	.
	.
 	while(gets(gt) != NULL) {
 		switch(gt[0]) {
 			case '1':
 				mesg[0].funcp(1);	/* (*...) absent */
 				break;
 			case '2':
 				mesg[1].funcp(2);	/* (*...) absent */
 				break;
 		}
 	}
	.
	.
	.
-- 

------------------------------------------------------------------------------

Doug East (Pacific*Bell -- San Francisco)
{ihnp4,ucbvax,cbosgd,decwrl,amd70,fortune,zehntel}!dual!ptsfa!dre