Page 1 of 1

[solved] Reading data from an array by pointer

Posted: Sun May 05, 2024 9:35 am
by ZX80
Hi, all.

This way I can read data from the array by the base pointer to it:

Code: Select all

Procedure ReadArray(*strings, size)
  *ptr = *strings
  For i=1 To size
    Debug PeekS(PeekL(*ptr))
    *ptr+SizeOf(integer)
  Next
EndProcedure

Dim Strings.s(5)
Strings(0)  = "AAA"
Strings(1)  = "AAB"
Strings(2)  = "AAC"
Strings(3)  = "ABA"
Strings(4)  = "ABB"

ReadArray(@strings(), 5)
So...
What if I have a structured array ? For example, I need to get both a string and an id at once.

Thank you in advance.

Re: Reading data from an array by pointer

Posted: Sun May 05, 2024 9:55 am
by infratec

Code: Select all

Structure Test_Structure
  String$
  Value.i
EndStructure

Procedure ReadArray(*strings.Test_Structure, size)
  Protected *ptr.Test_Structure
  
  *ptr = *strings
  For i=1 To size
    Debug *ptr\String$
    Debug *ptr\Value
    *ptr+SizeOf(Test_Structure)
  Next
EndProcedure

Dim Strings.Test_Structure(5)

Strings(0)\String$  = "AAA"
Strings(0)\Value = 0
Strings(1)\String$  = "AAB"
Strings(1)\Value = 1
Strings(2)\String$  = "AAC"
Strings(2)\Value = 2
Strings(3)\String$  = "ABA"
Strings(3)\Value = 3
Strings(4)\String$  = "ABB"
Strings(4)\Value = 4

ReadArray(@strings(0), 5)
Btw. since it is possible to use an Array as procedure parameter, this is not needed.

Re: Reading data from an array by pointer

Posted: Sun May 05, 2024 10:02 am
by ZX80
infratec,

thanks a lot. It works ! :D