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

Just starting out? Need help? Post your questions and find answers here.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
Last edited by Kaeru Gaman on Tue Apr 24, 2007 10:42 pm, edited 1 time in total.
oh... and have a nice day.
Clutch
User
User
Posts: 52
Joined: Sun Nov 26, 2006 6:11 am
Location: South Florida

Post 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
"Ahead one third... ahead two thirds... Full ahead flank
And out from the belly of the whale came a prophet, Amen"
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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. :)
oh... and have a nice day.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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.
Post Reply