Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

New to FreeBASIC? Post your questions here.
Makoto WATANABE
Posts: 195
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby Makoto WATANABE » Jul 13, 2020 10:04

Please teach me the difference between "Sadd", "StrPtr", "VarPtr", and "Address of" and how to use them properly.

Code: Select all

Dim ArrayStr(25) As String
Dim i As Integer
Dim Counter As Integer

Counter = 0

Print "文字列     " ;"変数へのポインタ " ;"文字列ポインタ ";"変数ポインタ ";"変数のアドレス"
Print "String        " ;"SAdd          " ;"StrPtr        ";"VarPtr        ";"Address of"
Print

For i = Asc("A") To Asc("Z")-5
   Counter = Counter + 1
   ArrayStr(Counter) = Chr(i, i+1, i+2, i+3, i+4)
   Print ArrayStr(Counter) ,SAdd(ArrayStr(Counter)) ,StrPtr(ArrayStr(Counter)) ,VarPtr(ArrayStr(Counter)) ,@ArrayStr(Counter)
Next i

Sleep
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby MrSwiss » Jul 13, 2020 13:58

It's pretty simple: StrPtr() and Sadd() (do the same on String's) while
VarPtr() and @ (do the same on numeric variables and/or objects)

All return a pointer ...
"on String" either ZString or WString (pointer)
"on numerics" a data-type specific (pointer)

Therefore, I'm only ever using: StrPtr() and @.
(SAdd() is to much misleading, and VarPtr() to long, to type)

Code: Select all

' Makoto_W.bas -- recoded by MrSwiss
'

Dim As String   alphab = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Print "String", "SAdd", "StrPtr", "VarPtr", "Address of"
Print

For i As UInteger = 0 To 25
   Print Chr(alphab[i]), SAdd(alphab), StrPtr(alphab), VarPtr(alphab[i]), @alphab[i]
Next

Sleep
Btw: I've removed the Japanese explanation ...
Makoto WATANABE
Posts: 195
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby Makoto WATANABE » Jul 14, 2020 14:11

Dear MrSwiss

Thank you for teaching me immediately.

After reading your answer, I looked at the FreeBASIC Manual and I understood the contents.

> I'm only ever using: StrPtr() and @.
I would like to imitate your behavior.

Thank you very much.
fxm
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby fxm » Jul 14, 2020 14:29

To complete the subject, there is also ProcPtr() which applied on the name of a procedure returns its address (@() does the same).

In summary, we can use @() everywhere, except for variable-length strings, because in this case @() returns the address of the string descriptor while the address of the string data characters cannot can only be provided only by StrPtr() (or Sadd()).
(for fixed-length strings, @() works because there is no descriptor for them)
Last edited by fxm on Jul 14, 2020 20:09, edited 2 times in total.
jj2007
Posts: 1693
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby jj2007 » Jul 14, 2020 18:50

fxm wrote:while the address of the string data characters cannot be provided only by StrPtr() (or Sadd())
That sounds ambiguous...
fxm
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby fxm » Jul 14, 2020 20:07

Indeed, thanks.
See my previous post corrected so.
dodicat
Posts: 6687
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby dodicat » Jul 14, 2020 20:32

@ is OK for variable length, it is @s[0]

Code: Select all

 
dim as string s="abcdefghij"
print strptr(s)
print @s
print @s[0]
print
dim as string * 10 g="abcdefghij"

print strptr(g)
print @g
print @g[0]
print


dim as zstring * 10 h="abcdefghij"

print strptr(h)
print @h
print @h[0]
print


print strptr("ghijklmnopqrstuvw")
print @"ghijklmnopqrstuvw"
print @("ghijklmnopqrstuvw")[0]
#print typeof("ghijklmnopqrstuvw")
sleep
 

in fact @s[0] is universal, it works for everything including literals.
fxm
Posts: 9946
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby fxm » Jul 14, 2020 20:56

It's a workaround because s[0] is a Ubyte, and @s[0] returns a Ubyte Ptr (not a (Z|W)String Ptr).
Makoto WATANABE
Posts: 195
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby Makoto WATANABE » Jul 15, 2020 1:53

Dear All !

Thank you all for teaching me so much.
I, a beginner, don't really understand "descriptor" and "string descriptor".
Then ,I feel dodicat's sample program to be very interesting.

If you could add these details to the FreeBASIC Manual in the future, it would make it easier to understand for beginners.
Thank you very much.
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Difference between "Sadd", "StrPtr", "VarPtr", and "Address of"

Postby MrSwiss » Jul 15, 2020 2:16

Makoto WATANABE wrote:I, a beginner, don't really understand "descriptor" and "string descriptor".
Have a look at the topic below, the Descriptor is in the Private: section of the Type:
a ZString Ptr & two UIntegers, a little bit different than Var-Len-String but similar.
Type FixLenStr (fixed size String Type)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 4 guests