Page 1 of 1

How to reference a String as both a string and an array

Posted: Tue Apr 24, 2007 10:23 pm
by Demivec
I would like to reference a string as an array of characters and as a string. I would like to do this without duplicating the string. I know that with pointers I can access the characters in a string by calculating a position (or offset) and looking at the value there. What I am searching for is a way to access the strings contents using the notation for an array.
Something like:

Code: Select all

string.s="Sample string"
dim array.c (0)
array=@string
debug array(5) ;displays an  "e"
for i.l=8 to 3 step -1  ;displays "ts elp"
  debug array(i)  
next i

Posted: Tue Apr 24, 2007 10:39 pm
by Kaeru Gaman
an array in PB has a header that states it's length.

so, you would need the header in front of the string to make your example work.
but there is no space for that left in the stringpool.

if you try it the other way round, redirect the stingpointer to the array,
you could overflow the arrayspace by setting the string.

the easiert solution I see atm is a structured union that holds an array and a fixed string...

Code: Select all

Structure CString
  StructureUnion 
    str.s{99}
    chr.c[99]
  EndStructureUnion
EndStructure

test.CString\str = "Sample string"

Debug Chr(test\chr[5]) ;displays an  "e"
For i.l=8 To 3 Step -1  ;displays "ts elp"
  Debug Chr(test\chr[i])
Next i 
but it would be much easier to directly access the mem of a simple string.
you could write a macro that calculates the pointer to every char.

Posted: Tue Apr 24, 2007 10:41 pm
by Clutch
I'm not sure what memory leaks there may be in doing this, but running this code does what you indicate:

Code: Select all

string.s="Sample string" 
dim array.s{1}(0) 
array()=@string
debug array(5) ;displays an  "e" 
for i.l=8 To 3 Step -1  ;displays "ts elp" 
  debug array(i)
next i

Posted: Wed Apr 25, 2007 10:20 am
by Trond
Fixed strings are not necessary:

Code: Select all

Structure S2
  s.c[0]
EndStructure

MyString.s = "Jackdaws loves my big sphinx of quartz"
*AsArray.S2 = @MyString
For I = 0 To Len(MyString)-1
  Debug Chr(*AsArray\s[I])
Next

Posted: Wed Apr 25, 2007 1:45 pm
by Kaeru Gaman
but this is not really an array..
you work with a structured pointer to trick it. ;)
in the end it's just a pointer to the string, but with better notation that can be used as an array.

nice hint, thnx. :)

Posted: Wed Apr 25, 2007 5:47 pm
by Demivec
@Trond: Thank you for your solution. I needed something that did not depend on declaring a structure for each possible length of a string. Your solution fits well.

After applying what you suggested to the example I started with, it looks like this using a Character structure:

Code: Select all

Structure CharacterArray
  s.c[0]
EndStructure

string.s="Sample string"
*AsArray.CharacterArray=@string
Debug Chr(*AsArray\s[5]) ;displays an  "e"
For I.l=8 To 3 Step -1  ;displays "ts elp"
  Debug Chr(*AsArray\s[I])
Next I 
or this using a static String structure (and without needing a Chr() function):

Code: Select all

Structure CharacterString
  s.s{1}[0]
EndStructure

string.s="Sample string"
*AsCstring.CharacterString = @string
Debug *AsCstring\s[5] ;displays an  "e"
For I.l=8 To 3 Step -1  ;displays "ts elp"
  Debug *AsCstring\s[I]
Next I 
Thanks for each of the replies.

*Edit:added example for using a static string structure from Clutch also.