passing pointers

BlitzMax Forums/BlitzMax Programming/passing pointers

dmaz(Posted 2015) [#1]
if I have a Ptr, how can I pass it to a function by the original reference?

that's not very clear.. :) here's some code which works except for the "other" method where I want to pass the Ptr I have. I get the following error:

Variable for 'Var' parameter is not of matching type

SuperStrict

Local v:Float = 10

Local i:t = New t
i.store v
i.add

Print v

Type t
	Field value:Float Ptr
	
	Method store( v:Float Var )
		value = Varptr v
	End Method
	
	Method add()
		value[0] :+ 1
	End Method
	
	Method other()
		Local i:t = New t
		i.store value
	End Method
End Type




Yasha(Posted 2015) [#2]
Well the code doesn't make sense since there's a type error: you are trying to pass a Float Ptr to a parameter expecting a Float.

You can get it to compile by changing the line in `other` to:

i.store value[0]


`value[0]` is a Float, and can thus be passed to where one is expected. This has nothing to do with whether `Var` is used. The whole point of `Var` is to completely conceal the use of pointers as an implementation detail, so there's no interaction with `Ptr` types.

Doing this gives the new `t` instance a pointer into the old one... if that's what you were actually trying to do.


Brucey(Posted 2015) [#3]
"value" is a Float Ptr.
store() accepts a Float.

They are not the same type.

You probably want to do something like this :
SuperStrict

Framework brl.standardio

Local v:Float = 10

Local i:t = New t
i.store Varptr v
i.add

Print v

Type t
	Field value:Float Ptr
	
	Method store( v:Float Ptr )
		value = v
	End Method
	
	Method add()
		value[0] :+ 1
	End Method
	
	Method other()
		Local i:t = New t
		i.store value
	End Method
End Type



Jesse(Posted 2015) [#4]
???
SuperStrict

Local v:Float = 10

Local i:t = New t
i.store v
i.add

Print v

Type t
	Field value:Float Ptr
	
	Method store( v:Float Var )
		value = Varptr(v)
	End Method
	
	Method add()
		value[0] :+ 1
	End Method
	
	Method other()
		Local i:t = New t
		i.store(value[0])
	End Method
End Type



dmaz(Posted 2015) [#5]
man, I was thinking ..[0] just returned the value.

so then, both versions work, Brucey's is probably more proper, right as it indicates what's going on? while using Var is cleaner as I don't need the Varptr when calling... hmmm

thanks!


Brucey(Posted 2015) [#6]
As Yasha says, Var hides all the pointer stuff away so you don't need to know what's going on :-)


Jesse(Posted 2015) [#7]
>>Brucey's is probably more proper, right as it indicates what's going on?
not necessarily but it's what you "think" it is.