"%#s"?

Stephen J. Friedl friedl at vsi.UUCP
Mon Jun 13 11:55:34 AEST 1988


In article <1156 at mcgill-vision.UUCP>, mouse at mcgill-vision.UUCP (der Mouse) writes:
> > [let's have spr_chr(int c) to return a stringized version of a char]
> 
> If you are going to use a static buffer, folks, please use several of
> them, or otherwise arrange that it doesn't lose big if I say
> 
> printf("  in_chr = %s, out_chr = %s\n",
> 	spr_chr(in_chr), spr_chr(out_chr));

For exactly this kind of thing we use a routine circbuf().  It has a
large static buffer and returns chunks to you upon request:

/*----------------------- circbuf.c ------------------------*/

#define		ASIZE		1024

char *
circbuf(size)
int	size;
{
static char	circarray[ASIZE],
		*nextfree = circarray;

	if ((nextfree + size) > &circarray[ASIZE])	/* enough room?	*/
		nextfree = circarray;			/* recycle	*/

	return((nextfree += size) - size));
}

/*----------------------- circbuf.c ------------------------*/

This is a handy malloc-like function that you don't have to free
up.  It strikes me as a little dangerous that you have to pay attention
to the lifetime of one of these strings (it will get overwritten
later), but we've not seen any problems with it.

-- 
Steve Friedl    V-Systems, Inc. (714) 545-6442      3B2-kind-of-guy
friedl at vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl

Nancy Reagan on ptr args with a prototype in scope: "Just say NULL"



More information about the Comp.lang.c mailing list