• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Equivalent of PB SWAP statement

Started by Chris Chancellor, June 14, 2018, 07:41:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles

PB has a swap statements which swap the values between 2 variables, what would be
the equivalent O2 statement ?

Please see
http://manmrk.net/tutorials/basic/PowerBASIC/pbcc5/swap_statement.htm

Thanxx

Eduardo Jorge

I think charles has already put an example of this in an answer to Mike
I believe it would be using pointer and swapping addresses with @
I think it would be something like this
A ,B
*P
@P=@A
@A=@B
@B=@P
in case instead of values exchange memory addresses

Charles Pegge

Hi Chris and Eduardo,

This will work for any primitive type, or flat UDT


  macro Swap(a,b,    c)
  =====================
  typeof(a) c=a
  a=b
  b=c
  end macro

  int a=1, b=2
  swap a,b

Chris Chancellor

Thanxx a lot both of you

this O2 Swap macro is even better than PB Swap,  bcos  PB Swap statement will require the same type of primitives
for the 2 variables a and b