Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!ames!ptsfa!ihnp4!homxb!mtuxo!mtune!codas!cpsc6a!rtech!wrs!dg From: dg@wrs.UUCP (David Goodenough) Newsgroups: comp.lang.c Subject: Re: Buffer referencing between functions. Message-ID: <431@wrs.UUCP> Date: Mon, 26-Oct-87 20:05:29 EST Article-I.D.: wrs.431 Posted: Mon Oct 26 20:05:29 1987 Date-Received: Sat, 31-Oct-87 06:39:10 EST References: <10756@sri-spam.istc.sri.com> Reply-To: dg@wrs.UUCP (David Goodenough) Organization: Wind River Systems, Emeryville, CA Lines: 58 Keywords: *(auto_variables) in calling functions? In article <10756@sri-spam.istc.sri.com> robert@sri-spam.istc.sri.com (Robert Allen) writes: > Below is a code fragment to illustrate my problem; > > struct stack_vars { > char *frombuf; > int frombuf_index; /* 0-2000 */ > char *tobuf; > int tobuf_index; /* 0-2000 */ > }; > > void calling(inbuf,inbuf_index) > char inbuf[]; > int inbuf_index; > { > struct stack_vars data; > char dbuf[2000]; > > data.frombuf = inbuf; > data.frombuf_index = inbuf_index; > data.tobuf = dbuf; > data.tobuf_index = 0; > called(&data); > } > > void called(sv) > struct stack_vars *sv; > { > bcopy(sv->frombuf,sv->tobuf,sv->frombuf_index); > sv->tobuf_index = sv->frombuf_index; > } Referencing dbuf via the pointer data.tobuf in the above section is legal, fair game, and I do similar sorts of things all the time. The main danger when passing pointers to locals around on the stack is NOT TO DO THIS: char *stug() { char blurf[100]; /* do something interesting */ return(blurf); } I.E. Don't return a pointer to a local variable, because by the time you use it it is no longer valid, at best it will still be there, at worst another procedure call will have overwritten it. -- dg@wrs.UUCP - David Goodenough ..... !rtech!--\ >--!wrs!dg ..... !sun!--/ +---+ | +-+-+ +-+-+ | +---+