Confussed: pointers from Kernigan & Richie

S

someone

On page 106 of the second edition of "The C Programming Language" by K
& G, two stack operations PUSH n POP are given.
*p++ = val;
val = *--p;

What I know about stacks is that the top of stack (tos) pointer pts to
the topmost element of the stack.
But going by the PUSH operation, the 'va' gets stored in *p first and
then the address of p is incremented by ++ which I think will
OVERWRITE the previous tos. Similarly with pop.

I thought about it and came to the following conclusion:

( either I am missing out on operator PRECENDENCE and its
associativity)

||

( the tos points always to an empty element just above the actual
element on top of the stack)

&&

( I am totally CONFUSSED :p)


please clear my doubts. Thanks in advance :)
 
J

Joona I Palaste

(e-mail address removed) scribbled the following:
On page 106 of the second edition of "The C Programming Language" by K
& G, two stack operations PUSH n POP are given.
val = *--p;
What I know about stacks is that the top of stack (tos) pointer pts to
the topmost element of the stack.
But going by the PUSH operation, the 'va' gets stored in *p first and
then the address of p is incremented by ++ which I think will
OVERWRITE the previous tos. Similarly with pop.
I thought about it and came to the following conclusion:
( either I am missing out on operator PRECENDENCE and its
associativity)

( the tos points always to an empty element just above the actual
element on top of the stack)

This is correct. The pointer will always point at an empty element,
which can be thought of the place where the *NEXT* element will be
pushed.
 
A

Arthur J. O'Dwyer

On page 106 of the second edition of "The C Programming Language" by K
& G, two stack operations PUSH n POP are given.

Do not multi-post. Your question has already been answered in
the newsgroup alt.comp.lang.learn.c-c++. Read it there.

-Arthur
 
S

someone

Do not multi-post. Your question has already been answered in
the newsgroup alt.comp.lang.learn.c-c++. Read it there.

i m learning :) . won't happen next time. thanks for ur reply though.

cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top