Kenneth said:
In Ansi-C C99 or Standard C or in GCC...
Is there a way to push a value onto the stack and then later retrieve it
within the same function?
void foo(void) {
.
.
push my value here;
.
.
pop my value here;
.
.
}
Or is this only machine dependant and/or compiler dependant?
Unlike Forth and related languages, C does not presuppose a stack. It
is perfectly feasible to write C compilers for machines that do not
support an intrinsic stack.
OTOH, many C's support inline assembler code. In such C's, executing on
machines with an intrinsic stack, you can easily implement what you
want.
However I would recommend if you do this, that when you exit a function
embodying direct stack manipulation, you restore the stack to its prior
state. Otherwise you risk misplacing data and/or crashing the system.
As someone has noted, it is perfectly feasible to implement a software
stack in C if you need such a data structure, and to define functions that
push/pop data to/from it. This would be by far the safest course. In
fact, it is one way to implement a recursive algorithm in a non-recursive
manner--e.g. QuickSort in Fortran 77.
--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/
"For there was never yet philosopher that could endure the toothache
patiently." -- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.