Pointer Types

BlitzMax Forums/BlitzMax Programming/Pointer Types

rdodson41(Posted 2005) [#1]
Using VarPtr on different types of variables produces different types of pointers??? Is there anyway to have VarPtr return an integer pointer no matter what type of subexpression is used? Meaning
num=1234
str$="hello"

do(VarPtr(num))
do(VarPtr(str))
End

Function do(p:Int Ptr)
   Print Var(p)
EndFunction


Since all pointers are integers, why can't the pointers to an integer and a string both be passed in as integers?


SJB(Posted 2005) [#2]
That makes perfect sense. When you ask for a pointer to a string it would be surprising if you got back a pointer to an integer.

Cast the pointer to a string to an int pointer if that is what you want:
do(Int Ptr(VarPtr(str))) 


Pointers may be stored as integers, but the whole point of types in a language is that things like pointers to ints and pointer to strings are different to the compiler.


ImaginaryHuman(Posted 2005) [#3]
I wonder, if is possible to get the varptr of an expression that uses varptr? like do(int ptr(varptr(varptr(str)))
?


rdodson41(Posted 2005) [#4]
Thanks SJB, I thought I heard some where that all pointers are integers, but that casting works the way i wanted.
Angel, this seems to work:
Local p1 Ptr, p2 Ptr, p3 Ptr Ptr

num=1234
str$="asdf"

p1=Int Ptr(VarPtr(num))
p2=Int Ptr(VarPtr(str$))
p3=VarPtr(p2)

p3 has to be an Int Ptr Ptr though which is wierd, since they are both jut pointers to a variable. I guess Int Pointer is like its own type such as int or float, so an Int Ptr needs its own pointer type, int ptr ptr.


Bot Builder(Posted 2005) [#5]
Strings internally are stored as 4 bytes pointing at a larger memory structure. Same with objects, arrays, other stuff like that. This is why these things are passed ByRef instead of ByVal. Its because the actual value of the variable passed is a pointer.