Assigning Structure address?

I’ve not done much beyond very simple stuff with declares, structures or pointers, so please forgive me if this is a stupid question!

if a declare returns a pointer to an array of n structures , as well as the number of structures in the array, and I have defined a Xojo structure for the array elements, can the starting address of of an Xojo structure variable be assigned somehow so i can iterate over the array using the xojo structure?

Usually you pass the Xojo structure variable byRef to the declare, but that obviously would not work for an array…If the # array elements were know at design time creating a Xojo Structure with an Array of structures would be the obvious solutions… but that is not the case…

I could use a memory block but that is a more code , less readable and just plain messy… Alternatively I could copy the correct #of bytes for each each structure in the array to the Xojo structure variable from a memoryblock … but I always hate copying data that should not need to be copied

What I want to be able to do, is something like this if possible:

[code]DIM aStruct as MyStruct
DIM ubStructArray as integer = UBoundArrayElementsFromDeclare
DIM PtrToArray as Ptr = FromDeclare

For i as integer = 0 to ubStructArray
aStruct.Address = PtrToArray
PtrToArray = PtrToArray + aStruct.Size
’ Process the data
Next
[/code]

but i don’t know if it is possible to assign an address to a Xojo Structure variable, or if it is, how to do it.

Thanks,
-Karen

you should be able to using a technique like this
https://www.great-white-software.com/blog/2019/07/16/c-unions/

Thanks Norm!!!

Either I missed it or the language reference on Ptr nor Structure never says that you can do:

DIM aStructVar as MyStruct = aPtr.MyStruct

So then my code would be:

[code]DIM aStruct as MyStruct
DIM ubStructArray as integer = UBoundArrayElementsFromDeclare
DIM PtrToArray as Ptr = FromDeclare

For i as integer = 0 to ubStructArray
aStruct = PtrToArray.MyStruct
PtrToArray = PtrToArray + aStruct.Size
’ Process the data
Next[/code]

This definitely should be in the docs!!!

It should be but I dont think this capability has ever been well understood by anyone writing the docs so it goes under documented
Thats been true for a long time though - long before docs became Pauls job

<https://xojo.com/issue/58030>