🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #5206

Re: function returning pointer to itself

#5206
From: T. William Wells
Date: Mon, 18 Jul 1988 09:52
53 lines
1286 bytes
As I recall, the original discussion was on how to create a state
machine using functions for the states.  Anyway, as has been
pointed out, the function can't return a pointer to itself.  What
it can do is to return a structure which contains a pointer to
itself.

The following code compiles and runs fine on our Sun.  It should
also be valid in Standard C.

typedef struct STR {
	struct STR (*func)();
} STR;

STR     Str;

STR     func1();
STR     func2();
STR     func3();

main()
{
	Str.func = func1;
	while (1) {
		Str = (*Str.func)();
	}
}

STR
func1()
{
	STR     str;

	printf("function 1\n");
	str.func = func2;
	return (str);
}

STR
func2()
{
	STR     str;

	printf("function 2\n");
	str.func = func1;
	return (str);
}

STR
func3()
{
	printf("function 3\n");
	exit(0);
}
Technical Details
Message-ID: <486@proxftl.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!att!rutgers!mailrus!uflorida!novavax!proxftl!bill
References: <7725@watdragon.waterloo.edu) <664@goofy.megatest.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!att!rutgers!mailrus!uflorida!novavax!proxftl!bill
From: bill@proxftl.UUCP (T. William Wells)
Newsgroups: comp.std.c
Subject: Re: function returning pointer to itself
Message-ID: <486@proxftl.UUCP>
Date: 18 Jul 88 09:52:55 GMT
References: <7725@watdragon.waterloo.edu) <664@goofy.megatest.UUCP>
Reply-To: bill@proxftl.UUCP (T. William Wells)
Organization: Proximity Technology, Ft. Lauderdale
Lines: 53