noob pointer question.

New to FreeBASIC? Post your questions here.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

noob pointer question.

Post by agamemnus »

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.

:)
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

1a. Modify FBC to support pointer to array.
1b. Recompile.
1c. Use array pointers.

2. Use and abuse a few MACROs.

3. Just use regular pointers.


Happy coding.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

But how would I "just use regular pointers" without making a huge amount of them?
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

Here is an example of using pointers and pointer-indexing to access various array contents:

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()
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.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

To follow your OP, here's another example:

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()
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.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

I'm lost.

I need to be able to do this for any value in the array, not just a particular one.

I guess a macro rewriter would work..
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

You can assign a pointer the address of any array element. You can also access any element in the array using pointer indexing. You may need to explain your problem a little clearer.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

In essence, I want to swap the arrays such that anything from the point of swappage makes array A actually mean array B and array B actually mean array A.
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Post by Fragmeister »

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.

Code: Select all

dim array1(30) as integer,array2(30) as integer
swap @array1,@array2
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.
Deleter
Posts: 975
Joined: Jun 22, 2005 22:33

Post by Deleter »

@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.

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
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

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:

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 
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

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
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Post by Fragmeister »

@frag, that doesn't work
I'm not surprised. That could cause all sorts of problems, if it was allowed to work... but I tried!
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

mambazo, I'm ready to stab you now.

Correct me if I'm wrong, but arrays are stored in chunks of memory I think, and I want to swap the starting memory references to the arrays.. ie I want to swap a and c in memory references a+b and c+d...hence pointers.
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Post by tunginobi »

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.
Post Reply