Pointer to String in Array an String in List! Whats differnt?

Just starting out? Need help? Post your questions and find answers here.
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Pointer to String in Array an String in List! Whats differnt?

Post by SMaag »

I have a Problem using Pointer to Strings in a String-Array and a String-List.
In the Array it is woriking as expected. In the List it is completely wrong.
What's the reason for?

Code: Select all

EnableExplicit

Define I, N, *ptr.Character

Dim ary.s(7)
NewList lst.s()


ary(0) = "abc"
ary(1) = "def"
ary(2) = "ghi"
ary(3) = "jkl"
ary(4) = "mno"
ary(5) = "pqr"
ary(6) = "stu"
ary(7) = "vwx"

Debug "-- Elements --"
For I = 0 To 7
  AddElement(lst())
  lst() = ary(I)
  Debug lst()
Next

Debug "-------"
Debug ""

Debug "Array"

For I=0 To 7
  *ptr = @ary(I)
  Debug ary(I) + " : " + Chr(*ptr\c) + " : " + Str(*ptr\c)
Next

Debug ""
Debug "List"
ForEach lst()
  *ptr = @lst()
  Debug lst() + " : " + Chr(*ptr\c) + " : " + Str(*ptr\c)
Next

here is the Debug output
-- Elements --
abc
def
ghi
jkl
mno
pqr
stu
vwx
-------

Array
abc : a : 97
def : d : 100
ghi : g : 103
jkl : j : 106
mno : m : 109
pqr : p : 112
stu : s : 115
vwx : v : 118

here the Pointer to not point to the String! Why?
List
abc : ঀ : 2432
def : ঠ : 2464
ghi : ী : 2496
jkl : ৠ : 2528
mno : ਀ : 2560
pqr : ਠ : 2592
stu : ੀ : 2624
vwx : ੠ : 2656
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Pointer to String in Array an String in List! Whats differnt?

Post by AZJIO »

Code: Select all

s = lst()
*ptr = @s
This won't work either

Code: Select all

s = @PeekS(*mem)
Last edited by AZJIO on Wed Mar 06, 2024 2:25 pm, edited 1 time in total.
Cyllceaux
Enthusiast
Enthusiast
Posts: 510
Joined: Mon Jun 23, 2014 1:18 pm

Re: Pointer to String in Array an String in List! Whats differnt?

Post by Cyllceaux »

Code: Select all

Debug ""
Debug "List"
ForEach lst()
  Define v.s=lst()
  *ptr = @v
  Debug lst() + " : " + Chr(*ptr\c) + " : " + Str(*ptr\c)
Next
The pointer on lst() does not point on the list value. It points on the list-Entry
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Pointer to String in Array an String in List! Whats differnt?

Post by SMaag »

s = lst()
*ptr = @s
Yes that is clear. If I copy the list into a string and take the pointer
of that String it works!

I did some tests and with, @lst.s() I get a PointerPointer, like .s is Structure containing a String! Like lst.String()

So handling as PointerPointer works! Looks like a Bug! But I'm not sure!
Generally it's confusing!

Code: Select all

EnableExplicit

Define I, N, *ptr.Character

Dim ary.s(7)
NewList lst.s()


ary(0) = "abc"
ary(1) = "def"
ary(2) = "ghi"
ary(3) = "jkl"
ary(4) = "mno"
ary(5) = "pqr"
ary(6) = "stu"
ary(7) = "vwx"

Debug "-- Elements --"
For I = 0 To 7
  AddElement(lst())
  lst() = ary(I)
  Debug lst()
Next

Debug "-------"
Debug ""

Debug "Array"

For I=0 To 7
  *ptr = @ary(I)
  Debug ary(I) + " : " + Chr(*ptr\c) + " : " + Str(*ptr\c)
Next

Debug ""
Debug "List"
ForEach lst()
  *ptr = PeekI(@lst.s())
  Debug lst() + " : " + Chr(*ptr\c) + " : " + Str(*ptr\c)
Next
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Pointer to String in Array an String in List! Whats differnt?

Post by AZJIO »

SMaag wrote: Wed Mar 06, 2024 2:31 pm PeekI
By the way, I forgot about this, although I asked it myself once. After all, copying a line wastes time.
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Pointer to String in Array an String in List! Whats differnt?

Post by mk-soft »

List element is a structure ...

Code: Select all


Global NewList text.s()

AddElement(text())
text() = "Hello"

AddElement(text())
text() = "World"

*element.integer

ForEach text()
  *element = @text()
  Debug PeekS(*element\i)
Next

*element_str.string

ForEach text()
  *element_str = @text()
  Debug *element_str\s
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Pointer to String in Array an String in List! Whats differnt?

Post by SMaag »

Thanks! Now it's clear for me!

A list entry has to be a Structure because it needs further data like PointerToNext and PointerToPrevious Element.
To realize this cost me some time of debugging!
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Pointer to String in Array an String in List! Whats differnt?

Post by mk-soft »

Show Purebasic SDK ... LinkedList

Code: Select all

;-TOP

Structure ListHeader
  *Next.ListHeader
  *Previous.ListHeader
EndStructure

Procedure GetNextElementPtr(*Element)
  Protected *List.ListHeader
  *List = *Element - SizeOf(ListHeader)
  If *List\Next
    ProcedureReturn *list\Next + SizeOf(ListHeader)
  Else
    ProcedureReturn #Null
  EndIf
EndProcedure

Procedure GetPreviousElementPtr(*Element)
  Protected *List.ListHeader
   *List = *Element - SizeOf(ListHeader)
  If *List\Previous
    ProcedureReturn *List\Previous + SizeOf(ListHeader)
  Else
    ProcedureReturn #Null
  EndIf
EndProcedure


Global NewList text.s()


AddElement(text())
text() = "1. Hello"

AddElement(text())
text() = "2. World"

AddElement(text())
text() = "3. ****"

*element_current.string
*element_previous.string
*element_next.string

ForEach text()
  *element_current = @text()
  ;Debug *element_str
  Debug "current: " + *element_current\s
  *element_previous = GetPreviousElementPtr(*element_current)
  *element_next = GetNextElementPtr(*element_current)
  If *element_previous
    Debug "previous: " + *element_previous\s
  Else
    Debug "previous nothing"
  EndIf
  If *element_next
    Debug "next: " + *element_next\s
  Else
    Debug "next nothing"
  EndIf
  Debug "----"
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply