[Gambas-user] About StrPtr()

Demosthenes Koptsis demosthenesk at ...626...
Sun Dec 12 19:38:47 CET 2010


ok i made this example

 PUBLIC SUB Main()
 
 DIM p AS Pointer
 DIM s AS String
 DIM a AS String = "this is a string" & Chr$(0)

 PRINT Len(a)
 PRINT InStr(a, Chr$(0))
  
 p = Alloc(a)
 
 'READ #p, s
 s = StrPtr(p)

 PRINT Len(s)
 PRINT InStr(s, Chr$(0))
 Free(p)

END
---------------------

Before StrPtr(p) then Len and InStr() shows 17 characters, ok!

After StrPtr(p) the last zero terminated character seems to be
disapeared.

The Len is 16 and InStr() does not find the chr$(0).

Did i do something wrong?

 

On Sun, 2010-12-12 at 19:01 +0100, Benoît Minisini wrote:
> > i just compared the output
> > with PRINT s and i saw the same result.
> > 
> > i was wrong because i did not know about string manipulation in c.
> > i did not understand the zero terminated string you mentioned in help.
> > 
> > i found some info at this page
> > http://www.dfstermole.net/OAC/hstrings.html
> > 
> > Is there any way to produce a zero terminated string in Gambas to
> > demostrate the StrPtr()?
> > 
> 
> Yes, but I don't want to really explain that, as it is internal interpreter 
> stuff.
> 
> Even if Gambas always knows the length of a string (by storing it with the 
> string contents for example), all strings created by Gambas are zero-
> terminated string. 
> 
> *But*, Gambas does not always create strings. For example, many string 
> manipulation functions (Mid$, Left$, Right$, Trim$...) do not really create 
> strings to be faster, and so do not return zero-terminated strings 
> necessarily.
> 
> So, to be sure, just create a string with an explicit Chr$(0) at its end.
> 
> Regards,
> 

-- 
Regards,
Demosthenes





More information about the User mailing list