Author Topic: Equivalent of PB SWAP statement  (Read 390 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 270
Equivalent of PB SWAP statement
« 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

Thanxx

Offline Eduardo Jorge

  • Jr. Member
  • **
  • Posts: 50
Re: Equivalent of PB SWAP statement
« Reply #1 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

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Equivalent of PB SWAP statement
« Reply #2 on: June 14, 2018, 11:22:51 PM »
Hi Chris and Eduardo,

This will work for any primitive type, or flat UDT

Code: [Select]
  macro Swap(a,b,    c)
  =====================
  typeof(a) c=a
  a=b
  b=c
  end macro

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

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 270
Re: Equivalent of PB SWAP statement
« Reply #3 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