Page 1 of 1

A question for C/C++ coders

Posted: Fri Jan 16, 2004 5:23 pm
by pythagoras_156
Hi,

I'm converting some C++ code to PB and, even though i have a little knowledge of C/C++, i have a doubt on the following lines :

Code: Select all

Stack[StackTop++]=PrevPixel;
Does it mean that the variable StackTop is actually incremented by 1 or does it only refer to the value StackTop + 1 ?
If the variable is really incremented, i would translate it like this in PB :

Code: Select all

StackTop+1 : Stack(StackTop) = PrevPixel 
if it's not incremented, like this :

Code: Select all

Stack(StackTop+1) = PrevPixel 
Is this correct ?

I also got the same problem with another line of code :

Code: Select all

RetVal = Stack[--StackTop]
Does the variable StackTop is decremented or is it only a reference to the value StackTop - 1 ?

If any C/C++ gurus could give me hint, i would be most grateful 8)

Pythagoras

Posted: Fri Jan 16, 2004 5:50 pm
by gkaizer
the

Code: Select all

Stack[StackTop++]=PrevPixel;
, means
take the current index of array/stack referred to by StackTop, assign the PrevPixel value and after that increment StackTop variable (increment the index)

AND

Code: Select all

RetVal = Stack[--StackTop]
decrements the StackTop variable by one and assigns a value contained in the Stack[StackTop with the new value] to RetVal

in PB i would translate that code like this

Code: Select all

Stack(StackTop)=PrevPixel
StackTop= StackTop+1
AND

Code: Select all

RetVal=Stack(StackTop-1)

Posted: Fri Jan 16, 2004 8:11 pm
by pythagoras_156
Thanks for your help. 8)

Pythagoras

Posted: Sat Jan 17, 2004 10:01 am
by dontmailme
Just to clarify in plain english......

If the ++ or -- is before the var i.e. ++var then it's incremented before

If it's after..... i.e. var++ then it's incremented after !

Quite simple once you know ;)[/b]

Posted: Mon Jan 19, 2004 1:39 am
by HASO
Pythagoras,

There is an error in the -- example: RetVal=Stack(StackTop-1)

Correct code:

StackTop = StackTop - 1
RetVal = Stack(StackTop)

HTH, Hans.

Posted: Mon Jan 19, 2004 1:56 am
by Dare2
Sort of on topic question:

With the automatic adding, eg

  var+10 ; var now incremented by ten

This would never happen if var was part of an expression, would it?

  varX = varA + 17 * (varB - 3 )

Would leave varA and varB unchanged, always. Surely?

Posted: Mon Jan 19, 2004 2:22 am
by HASO
Dare2,
varX = varA + 17 * (varB - 3 )
Would leave varA and varB unchanged, always. Surely?
Yes, it would. But remember the sequence for operators: multiplication before addition!

In C the operator ++ means: increment by 1 and -- means: decrement by 1.

If you put this operator before a variable it means: FIRST change the variable, then use it.
After variable means: FIRST use variable, then change it.

This rule is used for the current expression only. When you start a new expression the variable has the changed value.

Hope this helps,
Hans.

Posted: Mon Jan 19, 2004 2:41 am
by Dare2
Thanks. :)