Posted: 25th Mar 2004 12:31
Just thought I'd share this piece of code because a lot of people are unfamiliar with how to use pointers and what their capabilities are. This is one example. You pass two variables to the function and that function will then be able to modify these two values so you can essentially return two (or more if you wanted) values from a function rather then just one. This particular example swaps the two values with eachother.

+ Code Snippet
`Define Pointers and assign them some memory
Num1 As Dword
Num2 As Dword
Num1 = Make Memory(4)
Num2 = Make Memory(4)

`Now use any old values to swap
*Num1 = 14
*Num2 = 32

`Show what we have so far
Print "Num1: " + str$(*Num1)
Print "Num2: " + str$(*Num2)
Print "Swapping Values..."

`Call the function to swap them
Swap(Num1, Num2)

`Display new values
Print "Num1: " + str$(*Num1)
Print "Num2: " + str$(*Num2)

`Wait to quit
Wait Key

`cleanup our memory
Delete Memory Num1
Delete Memory Num2

`Exit
End

`This function will assign the memory addresses of the pointers to
`swap inside the local Dwords, then we can access them nice and easy
Function Swap(First As Dword, Second As Dword)

   `Now swap
   Temp As Dword
   Temp = *Second
   *Second = *First
   *First = Temp

EndFunction


Hope it's useful to someone.
Posted: 25th Mar 2004 21:12
did not know pointers exists in DBP , only knew about the memblock ptr() command, now I know
Posted: 26th Mar 2004 7:36
lol yeah, I only just learned of their existance in DBP recently as well. Hence why I decided to see if I could do that little C++ trick in DBP.
Posted: 26th Mar 2004 9:09
Woh, I never knew this. Thx for pointing this out.
Posted: 30th Mar 2004 23:57
wow, can you have a pointer to an array? Or pass an array in and out of a function like you can in C++?
Posted: 31st Mar 2004 12:21
arrays are global anyway, so why would you have too?
Posted: 31st Mar 2004 15:08
wow, can you have a pointer to an array? Or pass an array in and out of a function like you can in C++?


That's a good question. The problem I see with DBP is that you access memory like this:

PointName = Gets/Assigns Memory Address
*PointName = Gets/Assigns Value in Memory

And if it was a normal variable:

PointName = Gets/Assigns Value in Memory

So you can see it will have some clashes if it's not explicitly defined as a pointer (which is done by placing a memory address in it's value). In C++, it is:

PointName = Assigns Memory Address
&PointName = Gets Memory Address
*PointName = Gets/Assigns Value in Memory

And those will work with any variable basically, even arrays. This is why you can use pointers with arrays so easily. It's just:

Pointer = &ArrayName

Unfortunately, you can't do this in DBP because obviously it will just assign the value in ArrayName into the Pointer value. So, I'd have to say no, you can't do it with arrays (unfortunately), if someone has another idea though, I'd love to hear it.
Posted: 1st Apr 2004 14:25
You can have arrays pointer in DBPro with my array plug-in.

There are good reasons for doing this. If you have several arrays and need to perform the same operation on each you would currently need to write the code twice. With my plugin you can pass the array pointer to a single piece of code instead.

The only restriction with my plugin is that you cannot enlarge the array when you are using the pointer.
Posted: 2nd Apr 2004 16:39
aaahhh, sacrifice the dynamic array for allocated memory! Durn the laws! Durn them all.