Page 1 of 1
Pointer to String in Array an String in List! Whats differnt?
Posted: Wed Mar 06, 2024 2:16 pm
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
Re: Pointer to String in Array an String in List! Whats differnt?
Posted: Wed Mar 06, 2024 2:21 pm
by AZJIO
Re: Pointer to String in Array an String in List! Whats differnt?
Posted: Wed Mar 06, 2024 2:23 pm
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
Re: Pointer to String in Array an String in List! Whats differnt?
Posted: Wed Mar 06, 2024 2:31 pm
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
Re: Pointer to String in Array an String in List! Whats differnt?
Posted: Wed Mar 06, 2024 2:45 pm
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.
Re: Pointer to String in Array an String in List! Whats differnt?
Posted: Wed Mar 06, 2024 4:00 pm
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
Re: Pointer to String in Array an String in List! Whats differnt?
Posted: Fri Mar 08, 2024 11:53 am
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!
Re: Pointer to String in Array an String in List! Whats differnt?
Posted: Fri Mar 08, 2024 4:01 pm
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