This hack allows you to put a zero after a string temporarily,
i.e., just long enough to use printf.
Calling pushchar(p) is equivalent to writing *p = '\0',
except that calling popchar() undoes the effect of the last
pushchar.
The stack is shallow, and the program halts with an assertion failure
if it overflows.
<header>= (U->)
extern void pushchar(char *p);
extern void popchar(void);
Here's the implementation.
<*>=
#include <assert.h>
<header>
static char stack[3];
static char *pstack[3];
static int next = 0;
void pushchar(char *p) {
assert(next < sizeof(stack));
stack[next] = *p;
pstack[next++] = p;
*p = 0;
}
void popchar(void) {
char *p;
assert(next > 0);
p = pstack[--next];
*p = stack[next];
}
Defines next, popchar, pstack, pushchar, stack (links are to index).