Page 1 of 1

[Solved] List the fields of the structure

Posted: Sun Oct 19, 2025 8:28 pm
by ZX80
Hi, all.

Is it possible to list all the fields of a structure in a loop if their number and type are known in advance ?

Re: List the fields of the structure

Posted: Sun Oct 19, 2025 9:45 pm
by STARGĂ…TE
ZX80 wrote: Sun Oct 19, 2025 8:28 pm Hi, all.

Is it possible to list all the fields of a structure in a loop if their number and type are known in advance ?
No, because all structures are known (for the compiler) during compilation.
But even if so, how would you want to access this field then?

The only option is, you check with SizeOf(Structure) the size and with TypeOf(Structure\Field) and OffsetOf() the type and offset.
However, even here you need to know the name.

Re: List the fields of the structure

Posted: Sun Oct 19, 2025 11:55 pm
by ChrisR
There may be a workaround using the JSON library

Code: Select all

Structure Person
  FirstName.s
  LastName$
  Age.l
  List Books.s()
EndStructure

Define P.Person
P\FirstName = "John"
P\LastName$  = "Smith"
P\Age       = 42
AddElement(P\Books()): P\Books() = "Investing For Dummies"
AddElement(P\Books()): P\Books() = "English Grammar For Dummies"
AddElement(P\Books()): P\Books() = "A Little Bit of Everything For Dummies"

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @P, Person)
  JsonData$ = ComposeJSON(0)
  FreeJSON(0)
  Debug "JSON data = " + JsonData$
  Debug "--- List Structure fields (unsorted) ---"
  If ParseJSON(0, JsonData$)
    ObjectValue = JSONValue(0)
    
    If ExamineJSONMembers(ObjectValue)
      While NextJSONMember(ObjectValue)
        Select JSONType(JSONMemberValue(ObjectValue))
          Case #PB_JSON_Null    : Debug JSONMemberKey(ObjectValue) + " (Null)"
          Case #PB_JSON_String  : Debug JSONMemberKey(ObjectValue) + " (String) = " +  GetJSONString(JSONMemberValue(ObjectValue))
          Case #PB_JSON_Number  : Debug JSONMemberKey(ObjectValue) + " (Number) = " +  StrD(GetJSONDouble(JSONMemberValue(ObjectValue)))    
          Case #PB_JSON_Boolean : Debug JSONMemberKey(ObjectValue) + " (Boolean) = " +  Str(GetJSONBoolean(JSONMemberValue(ObjectValue)))
          Case #PB_JSON_Array   : Debug JSONMemberKey(ObjectValue) + " (Array)"
            ;For i = 0 To JSONArraySize(JSONMemberValue(ObjectValue)) - 1
            ;  Debug "  " + GetJSONString(GetJSONElement(JSONMemberValue(ObjectValue), i))
            ;Next
          Case #PB_JSON_Object  : Debug JSONMemberKey(ObjectValue) + " (Object)"
        EndSelect
      Wend
    EndIf
    FreeJSON(0)
  EndIf
EndIf

Re: List the fields of the structure

Posted: Mon Oct 20, 2025 5:36 am
by AZJIO
I think you need to change the structure to a list

Code: Select all

Structure Person
  FirstName.s
  LastName$
  Age.s
  List *Books()
EndStructure

Define P.Person
P\FirstName = "John"
P\LastName$  = "Smith"
P\Age       = "42"
AddElement(P\Books()): P\Books() = @P\FirstName
AddElement(P\Books()): P\Books() = @P\LastName$
AddElement(P\Books()): P\Books() = @P\Age

ForEach P\Books()
	Debug PeekS(P\Books())
Next
Since the data type must be the same, you can simply read the data by moving the pointer to the same offset.

I don't understand why it doesn't work.

Code: Select all

Structure Person
  FirstName.s
  LastName$
  Age.s
  Age2.s
EndStructure

Define P.Person
P\FirstName = "John"
P\LastName$  = "Smith"
P\Age       = "42"
P\Age2       = "423"

*Start = @P\FirstName
For i = *Start To *Start + SizeOf(Person) - SizeOf(Integer) Step SizeOf(Integer)
; 	Debug i
	Debug PeekS(i)
Next

Re: List the fields of the structure

Posted: Mon Oct 20, 2025 7:28 am
by Demivec
AZJIO wrote: Mon Oct 20, 2025 5:36 am I don't understand why it doesn't work.
It is probably due to the structured string element and the quirk in the functioning of PureBasic's @ operator returning the address of the string's content instead of the address of the pointer to the string's content.

Try this untested code:

Code: Select all

Structure Person
  FirstName.s
  LastName$
  Age.s
  Age2.s
EndStructure

Define P.Person
P\FirstName = "John"
P\LastName$  = "Smith"
P\Age       = "42"
P\Age2       = "423"

*Start = @P; @P\FirstName  = address of string contents
For i = *Start To *Start + SizeOf(Person) - SizeOf(Integer) Step SizeOf(Integer)
; 	Debug i
	Debug PeekS(PeekI(i))
Next

Re: List the fields of the structure

Posted: Mon Oct 20, 2025 12:35 pm
by ZX80
This is simply a holiday for the soul !
Unbelievable ! Folk, thank you so much ! To everyone ! Your support is worth a lot! I am simply the happiest and satisfied person today ! Thank you again ! You are the POWER & FORCE !

Re: [Solved] List the fields of the structure

Posted: Mon Oct 20, 2025 2:44 pm
by ChrisR
Extending AZJIO, Demivec example for others basic type structure fields, know in advance!
But it's not very pretty, it is hard-coded for each field/type

Code: Select all

Structure Person
  FirstName.s
  Age.d
  LastName${30}
  Age2.l
  SubName$
  Age3.i
EndStructure

Define P.Person
P\FirstName = "John"
P\Age       = 42
P\LastName$  = "Smith"
P\Age2       = 423
P\SubName$   = "J.Smith"
P\Age3       = 4235

*Start = @P
For I = 1 To 6
  Select I
    Case 1 ;String 
      Debug PeekS(PeekI(*Start)) : *Start + SizeOf(Integer)
    Case 2 ;Double
      Debug PeekD(*Start) : *Start + SizeOf(Double)
    Case 3 ; Fixe String
      Debug PeekS(*Start) : *Start + 30*SizeOf(Character)
    Case 4 ;Long
      Debug PeekL(*Start) : *Start + SizeOf(Long)
    Case 5 ;String
      Debug PeekS(PeekI(*Start)) : *Start + SizeOf(Integer)
    Case 6 ;Integer
      Debug PeekI(*Start) : *Start + SizeOf(Integer)
  EndSelect
Next

Re: [Solved] List the fields of the structure

Posted: Tue Oct 21, 2025 11:54 am
by ZX80
ChrisR

Perfect !
Thank you !
But it's not very pretty, it is hard-coded for each field/type
It doesn't matter. I said from the start that the types and number of fields are known in advance. So... everything's fine.