a basic like gosub in C

Bruce Holloway holloway at drivax.UUCP
Thu Feb 27 04:05:32 AEST 1986


In article <202 at crunch.uucp> josh at crunch.uucp (Josh Siegel) writes:
>In basic there is a GOSUB routine that will push the current
> position onto the stack and jump to another line.  Then, upon
> hitting a return will return to that place.  I wish to write
> something like that in C that will be movable between compilers.

(Sorry I'm not sending MAIL... but it doesn't seem to work from "rn" under
the C shell....)

You should use the setjmp() and longjmp() routines.

Call setjmp with a implementation specific environment buffer. When a
longjmp is called with the same buffer, it returns from the setjmp with
a return code. setjmp returns zero in a normal routine, or the longjmp 
parameter if that's wwhere it came from. Hmmm.

	jmp_buf env;

	func(){
	    :
	    :
	    if(setjmp(env)){

/* Code executed when returning from LONGJMP */

		}
	    :
	    :
	    longjmp(env,ret_code);
	    }


-- 

+----------------------------------------------------------------------------+
|Whatever I write are not the opinions or policies of Digital Research, Inc.,|
|and probably won't be in the foreseeable future.                            |
+----------------------------------------------------------------------------+

Bruce Holloway

....!ucbvax!hplabs!amdahl!drivax!holloway
(I'm not THAT Bruce Holloway, I'm the other one.)



More information about the Comp.lang.c mailing list