Author Topic: [SOLVED] How to Swap Array References?  (Read 1910 times)

0 Members and 1 Guest are viewing this topic.

Mike Lobanovsky

  • Guest
[SOLVED] How to Swap Array References?
« on: September 17, 2014, 10:46:03 AM »
Charles,

How can I swap the references of two Oxygen arrays?

Say, there are two int arrays of same size, array0[] and array1[]. At some point in my program I want to swap the references (names) of these arrays so that array0 would point to the contents of array1 and vice versa.

Is creating overlays and swapping the addresses that these overlays are pointing to the only way feasible to achieve this effect?

Thanks.
« Last Edit: September 17, 2014, 07:54:37 PM by Mike Lobanovsky »

JRS

  • Guest
Re: How to Swap Array References?
« Reply #1 on: September 17, 2014, 11:32:50 AM »
Maybe something like the Script BASIC REF would work with OxygenBasic. I don't under estimate the power of pointer/macro magic.


Charles Pegge

  • Guest
Re: How to Swap Array References?
« Reply #2 on: September 17, 2014, 06:53:14 PM »
Yes, overlays in some form are required. Their base addresses can be swapped.

Example:
Code: [Select]
INCLUDEPATH "$\inc\"
INCLUDE "Console.inc"
 
macro refswap(a,b)
scope
  let _a=@b
  @b=@a
  @a=_a
end scope
end macro
 
float as1[100],as2[100]   'STATIC ARRAYS

float *ar1=@as1,*ar2=@as2 'ARRAYS BYREF

'TEST
ar1[5]=1.25
printl ar1[5] "  " ar2[5]
refswap(ar1,ar2)
printl ar1[5] "  " ar2[5]
waitkey

Mike Lobanovsky

  • Guest
Re: How to Swap Array References?
« Reply #3 on: September 17, 2014, 07:53:26 PM »
@Charles:

Thanks much for the hint. I just wanted to be sure there's no better way. There's so much in Oxygen still hidden from the eyes of an ordinary user... :)


@John:

Yes, Oxygen's "overlays" are pretty much like SB's "ref vars" and they serve similar purposes.