noob pointer question.
noob pointer question.
I have 2 arrays. How do I create a pointer to swap between the arrays? Example:
if array1(index1) = A then array2(index2) = B
I want to create a pointer such that it does the opposite:
if array2(index1) = A then array1(index2) = B
How would I do that? Thanks.
:)
if array1(index1) = A then array2(index2) = B
I want to create a pointer such that it does the opposite:
if array2(index1) = A then array1(index2) = B
How would I do that? Thanks.
:)
Here is an example of using pointers and pointer-indexing to access various array contents:
Note that you can assign the address of any array index, not just the first one.
pArray = @Array1(1) would assign pArray the address of the second integer of Array1, therefore,
pArray[ 2 ] would actually be accessing the 4th integer in the array.
Code: Select all
function PointerAndArrayExample() as integer
dim as integer Array1(3) => { 0,1,2,3 }
dim as integer Array2(1,3) => { {0,1,2,3},{4,5,6,7} }
dim as integer ptr pArray => 0
'' assign pArray the address of the Array1's 1st integer
'' and access the 3rd integer using pointer indexing
pArray = @Array1(0) : print pArray[ 2 ]
'' assign pArray the address of the Array1's 1st integer
'' and access the 7th integer using pointer indexing
pArray = @Array2(0,0) : print pArray[ 6 ]
sleep : return 0
end function
end PointerAndArrayExample()
pArray = @Array1(1) would assign pArray the address of the second integer of Array1, therefore,
pArray[ 2 ] would actually be accessing the 4th integer in the array.
To follow your OP, here's another example:
You must be careful - as always - when working with these pointers since you have no bounds checking anymore. If you need some, store the size of the arrays in a seperate variable, or store an extra terminating object at the end of the array which you check against when accessing the array.
Code: Select all
function PointerAndArrayExample2() as integer
dim as integer Array1(3) => { 0,1,2,3 }
dim as integer Array2(1,3) => { {0,1,2,3},{4,5,6,7} }
'' assign both pointers to their respective arrays
dim as integer ptr pArray1 => @Array1(0)
dim as integer ptr pArray2 => @Array2(0,0)
'' test Array1 and possibly assign Array2
if( pArray1[1] = 2 ) then pArray2[3] = 4
'' swap pointers so they refer to opposite arrays
swap pArray1, pArray2
'' using same pointers, refer to opposite arrays
if( pArray1[1] = 2 ) then pArray2[3] = 4
sleep : return 0
end function
end PointerAndArrayExample2()
-
- Posts: 545
- Joined: Nov 08, 2005 14:36
Here's a way that SHOULD work, but both arrays have to be the same size, i.e., the same number of elements and the same type.
I have no idea if fb will let you do this, however... But it would have the effect of swapping all of the elements in array1 with all of the corresponding elements in array2.
Code: Select all
dim array1(30) as integer,array2(30) as integer
swap @array1,@array2
@frag, that doesn't work, you need to specify the array element when doing that so you would have to use UBOUND(array) and a for next to do each value.
Alternatively, if you want to venture into the realm of dynamic memory, here is a short example which does what you want using pointers and callocate, which basically attaches memory to your pointer to be used.
Alternatively, if you want to venture into the realm of dynamic memory, here is a short example which does what you want using pointers and callocate, which basically attaches memory to your pointer to be used.
Code: Select all
'make pointers to be used for the arrays
dim array1 as integer ptr, array2 as integer ptr
dim as integer arraysize
'assign the amount of memory needed to store 30 integer values
'one is added for figuring out size (not necessary but helpful)
arraysize=30
array1=callocate((arraysize+1)*sizeof(integer))
array2=callocate((arraysize+1)*sizeof(integer))
'usage requires []'s, not ()'s
'set the first value equal to the size of the array
array1[0]=arraysize
array2[0]=arraysize
'note that 1 is the first element, NOT 0!
'1 to array[0] is the whole array since array[0] contains the size of the array
for tmp = 1 to array1[0]
array1[tmp]=tmp
next
for tmp = 1 to array2[0]
array2[tmp]=31-tmp
next
width 80, 50
print "before"
print " a: b"
for tmp = 1 to array1[0]
print array1[tmp];":";array2[tmp]
next
swap array1, array2
sleep
print "after"
print " a: b"
for tmp = 1 to array1[0]
print array1[tmp];":";array2[tmp]
next
sleep
Ok, is there something simpler than that? For instance, say I have an array3, and pointers ptrarray1 and ptrarray2, which start in the beginning and the (middle+1) of array3.
Then what I want to do is:
do that let me jump 1 integer every 1 increase in ptrarray1 and then I could swap array1 and array2..
Reason I'm asking this is because I just don't have a clue what this is for:
Then what I want to do is:
do that let me jump 1 integer every 1 increase in ptrarray1 and then I could swap array1 and array2..
Reason I'm asking this is because I just don't have a clue what this is for:
Code: Select all
dim as integer arraysize
'assign the amount of memory needed to store 30 integer values
'one is added for figuring out size (not necessary but helpful)
arraysize=30
array1=callocate((arraysize+1)*sizeof(integer))
array2=callocate((arraysize+1)*sizeof(integer))
'usage requires []'s, not ()'s
'set the first value equal to the size of the array
array1[0]=arraysize
array2[0]=arraysize
Um, not sure entirely what you need to do but, perhaps this helps?
Code: Select all
DIM AS INTEGER Array1(10), Array2(10), i
' fill array1
FOR i = 0 TO 10
Array1(i) = i
NEXT i
PRINT "here are our arrays to start with!"
SLEEP
' display the arrays contents
FOR i = 0 TO 10
PRINT "Array1(";i;") = "; Array1(i),"Array2(";i;") = "; Array2(i)
NEXT i
PRINT
PRINT "Prepare to swap!"
PRINT
SLEEP
' this will swap all elements of the arrays
FOR i = 0 TO 10
SWAP Array1(i), Array2(i)
NEXT i
' display the arrays contents
FOR i = 0 TO 10
PRINT "Array1(";i;") = "; Array1(i),"Array2(";i;") = "; Array2(i)
NEXT i
PRINT
PRINT "Prepare to swap just the last 5!"
PRINT
SLEEP
' this will swap the last 5 elements of the arrays
FOR i = 5 TO 10
SWAP Array1(i), Array2(i)
NEXT i
' display the arrays contents
FOR i = 0 TO 10
PRINT "Array1(";i;") = "; Array1(i),"Array2(";i;") = "; Array2(i)
NEXT i
SLEEP
-
- Posts: 545
- Joined: Nov 08, 2005 14:36
Why not just have two arrays of pointers? Dynamically allocate the data, and just point to that data from the pointers in the arrays.
I admit that your use of English is making the problem less clear than it could possibly be, but this way, the data goes directly into memory, while you can keep tabs on it using your arrays.
Hence your expressions would look something like *a + *b and *c + *d. Using arrays, that might look like *a[0] + *b[0], if a and b are arrays of pointers, of which each entry contains the address of a dynamically allocated piece of data.
I admit that your use of English is making the problem less clear than it could possibly be, but this way, the data goes directly into memory, while you can keep tabs on it using your arrays.
Hence your expressions would look something like *a + *b and *c + *d. Using arrays, that might look like *a[0] + *b[0], if a and b are arrays of pointers, of which each entry contains the address of a dynamically allocated piece of data.