A question for C/C++ coders

Just starting out? Need help? Post your questions and find answers here.
pythagoras_156
User
User
Posts: 13
Joined: Wed Aug 20, 2003 11:08 am

A question for C/C++ coders

Post 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
gkaizer
User
User
Posts: 40
Joined: Fri Oct 31, 2003 2:57 pm
Location: Italy

Post 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)
--:: Gkaizer ::--
Using PB 3.92 on WinXP SP2 and 3.91 on Mandrake Linux 10.0
pythagoras_156
User
User
Posts: 13
Joined: Wed Aug 20, 2003 11:08 am

Post by pythagoras_156 »

Thanks for your help. 8)

Pythagoras
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post 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]
Paid up PB User !
HASO
User
User
Posts: 14
Joined: Tue Jun 10, 2003 12:29 am
Location: Netherlands

Post by HASO »

Pythagoras,

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

Correct code:

StackTop = StackTop - 1
RetVal = Stack(StackTop)

HTH, Hans.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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?
HASO
User
User
Posts: 14
Joined: Tue Jun 10, 2003 12:29 am
Location: Netherlands

Post 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.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Thanks. :)
Post Reply