Can you change a variables pointer?

BlitzMax Forums/BlitzMax Programming/Can you change a variables pointer?

Ziltch(Posted 2006) [#1]
Can Blitzmax modify an existing variables pointer?

i.e.
local a = 10
local b = 25

Can we change 'a' pointer so it is looking at 'b'?
This is very important for returned pointers from a Dll to an existing type structure in memory.

Getting pointers with varptr is easy , changing them is not!


ImaginaryHuman(Posted 2006) [#2]
Yeah. You'd need to use some pointer variables alongside them though. Or put them in a type.

local a=10
local b=25

would need..

local c:Int Ptr=varptr(a)
local d:Int Ptr=varptr(b)

Then think of your variable with c[0] and d[0] instead of a and b

ie

c=d ' make a look at b
print c[0] ' same as print a
print d[0] ' same as print b
c[0]=5 ' same as a=5
d[0]=15 ' same as b=15


ImaginaryHuman(Posted 2006) [#3]
Function pointers can do more of what you're thinking of , more directly changing to point to another function. I don't think you can change variable pointers, they are internal, without using a explicit pointer variable to manage it manually. I expect Max uses a pointer behind the scenes which is not accessible to your app.


Ziltch(Posted 2006) [#4]
Thank you for your info.

I will have to find anothewr way!


FlameDuck(Posted 2006) [#5]
Can we change 'a' pointer so it is looking at 'b'?
No.
This is very important for returned pointers from a Dll to an existing type structure in memory.
But you can do that.


Difference(Posted 2006) [#6]
Have you looked at CreateStaticbank() ?

You can point that at an existing memoryblock.


ImaginaryHuman(Posted 2006) [#7]
You can cludge a static bank onto a variable

Local a:Int=5
Local bnk:TBank=CreateStaticBank(VarPtr(a),4)
Local buf:Int Ptr=Int Ptr(BankBuf(bnk))
buf[0]=10 'same as a=10
print buf[0] 'same as print a


Fabian.(Posted 2006) [#8]
Ziltch:
local a = 10
local b = 25
You could write:
local a[] = [10]
local b[] = [25]
If you now want to change a so that it's looking at b you can write:
a = b
To access to the values of the variables use a[0] instead of a and b[0] instead of b.
This allows you two ways of setting the variable to a value:
a[0]=8 'This sets a and all variables looking at a to 8.
a=[8] 'This sets only a to 8; other variables looking at a will stay at their old values.

Ziltch:
Getting pointers with varptr is easy , changing them is not!
A pointer to a variable is a constant - so you can't change it.


Ziltch(Posted 2006) [#9]
Thanks for the handy info.
The CreateStaticBank command may help. Thanks Peter and AngelDaniel.
Fabian , I also did not know you could do , a=[8] . Thanks!