Powerbasic Museum 2020-B

IT-Consultant: Charles Pegge => OxygenBasic => Topic started by: Chris Chancellor on June 14, 2018, 07:41:34 PM

Title: Equivalent of PB SWAP statement
Post by: Chris Chancellor on June 14, 2018, 07:41:34 PM
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 (http://manmrk.net/tutorials/basic/PowerBASIC/pbcc5/swap_statement.htm)

Thanxx
Title: Re: Equivalent of PB SWAP statement
Post by: Eduardo Jorge on June 14, 2018, 08:35:23 PM
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
Title: Re: Equivalent of PB SWAP statement
Post by: Charles Pegge on June 14, 2018, 11:22:51 PM
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
Title: Re: Equivalent of PB SWAP statement
Post by: Chris Chancellor on June 15, 2018, 12:38:00 AM
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