where does VarPtr point to?

BlitzMax Forums/BlitzMax Programming/where does VarPtr point to?

Rozek(Posted 2007) [#1]
Hello!

Lets assume that I have two variables

StringVar:String
ArrayVar:Byte[]

In that case, will

VarPtr StringVar
VarPtr ArrayVar

point to the first character in StringVar or the first element in ArrayVar, resp.? Both types are actually Objects, which means that VarPtr could yield the address of s.th. else...

(FYI: I'm asking because I'll have to pass pointers to external C functions and would like to know if I'll have to do some addtional preparations or may pass the VarPtr's as such - don't worry about "length" issues: the "length" is always passed separately)


Rozek(Posted 2007) [#2]
Hmmm,

I think I found a different approach to solve the underlying problem - but (for academic reasons) the question persists...


grable(Posted 2007) [#3]
Varptr gives you the pointer to the variable itself, like & in c.

Passing the array would suffice as its just a pointer to its first element anyway.

Strings has to be passed via a $z parameter or by using .ToCString() (dont forget to MemFree() it when your done!).


Brucey(Posted 2007) [#4]
If you had an int,

Local x:int = 5

and you had a C function - void myFunc( int x )
Extern
    Function myFunc( x:Int )
End Extern

you could pass x into the function with

myFunc(x)

If the C function was - void myFunc( int * x )
Extern
    Function myFunc( x:Int Ptr )
End Extern

you could pass it into the function with

myFunc( Varptr x)


For Strings... it depends on the context. I've found occasions where I can define my Extern as myString:Byte Ptr, and simply pass the string into the function itself.
In other places I convert to a CString first...


Azathoth(Posted 2007) [#5]
What they said though if the C functions use wide chars use WString instead of CString.


Rozek(Posted 2007) [#6]
Thanks for your posts!

Indeed, a Byte[] seems to be the same as a pointer to its first element - the following test illustrates this:
local Text:Byte[] = [byte(0),byte(1),byte(2),byte(3),byte(4),byte(5),byte(6),byte(7)]
local TextPtr:Byte Ptr = Text
print(TextPtr[0]+","+TextPtr[1]+","+TextPtr[2]+","+TextPtr[3])
print(TextPtr[4]+","+TextPtr[5]+","+TextPtr[6]+","+TextPtr[7])

yields

0,1,2,3
4,5,6,7

as expected. On the other side, a similar code
local Text:String = "abc~0def"
local TextPtr:Byte Ptr = Text

is rejected by the compiler - and
local Text:String = "abc~0def"
local TextPtr:Byte Ptr = VarPtr Text
print(TextPtr[0]+","+TextPtr[1]+","+TextPtr[2]+","+TextPtr[3])
print(TextPtr[4]+","+TextPtr[5]+","+TextPtr[6]+","+TextPtr[7])

produces

16,211,102,0
104,255,168,0

which is *not* what you would expect.


Dreamora(Posted 2007) [#7]
A string is a string.
It is a special object no array of char like in C nor is a VAR an actual pointer that you can use on arrays like in C. BM is managed and can if needed move and resort data where needed so if you use pointer, always be aware that they will cause you many problems of done within BM or if you rely on "long term aliveness".

If you need array of char, you have to use the above mentioned CString and WString functions and use them.