Confussed: Pointers in 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 :)
 
R

red floyd

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 :)

The second case is correct. Not having my copy of K&R2 handy, my
interpretation is that in their sample, p points to the empty element
above the actual stack top.
 
V

Victor Bazarov

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.

In the beginning there are no elements stored. In the beginning
'p' point to the beginning of the stack memory area. So, 'p'
always points to _where_ the next push should store the data, or
"one past the top of the stack".

If you point it always at the "top element", then there is no way
to determine that the stack is empty.

PUSH operation initiates the increment, then uses the _old_ value
of the pointer (that's why the _post_-increment is used) to store
the value. As the result, the value is stored and the pointer 'p'
now points to the next element.

POP does the reverse. It _decrements_ the pointer and then extracts
the value to which the pointer points _after_ decrement (and that's
why _pre_-decrement is used).
I thought about it and came to the following conclusion:

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

That I don't know.
||

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


&&

( I am totally CONFUSSED :p)

I don't know about CONFUSSED, but _confused_ you probably were.

Victor
 
I

Ioannis Vranos

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 :)



In addition to what others said, keep in mind that there is a C specific
newsgroup called comp.lang.c . C++ and C are different languages.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
S

someone

In the beginning there are no elements stored. In the beginning
'p' point to the beginning of the stack memory area. So, 'p'
always points to _where_ the next push should store the data, or
"one past the top of the stack".

If you point it always at the "top element", then there is no way
to determine that the stack is empty.

PUSH operation initiates the increment, then uses the _old_ value
of the pointer (that's why the _post_-increment is used) to store
the value. As the result, the value is stored and the pointer 'p'
now points to the next element.

POP does the reverse. It _decrements_ the pointer and then extracts
the value to which the pointer points _after_ decrement (and that's
why _pre_-decrement is used).


That I don't know.


I don't know about CONFUSSED, but _confused_ you probably were.

Victor


Thank you guys for the quick responses :) . I really appreciate it.
 
D

Default User

Ioannis said:
In addition to what others said, keep in mind that there is a C specific
newsgroup called comp.lang.c . C++ and C are different languages.

Yes, he multi-posted in there too. Bad someone, bad.




Brian Rodenborn
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top