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 ?
[Solved] List the fields of the structure
[Solved] List the fields of the structure
Last edited by ZX80 on Mon Oct 20, 2025 12:36 pm, edited 1 time in total.
Re: List the fields of the structure
No, because all structures are known (for the compiler) during compilation.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 ?
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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: List the fields of the structure
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
EndIfRe: List the fields of the structure
I think you need to change the structure to a list
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
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())
NextI 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
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
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 !
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
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
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
NextRe: [Solved] List the fields of the structure
ChrisR
Perfect !
Thank you !
Perfect !
Thank you !
It doesn't matter. I said from the start that the types and number of fields are known in advance. So... everything's fine.But it's not very pretty, it is hard-coded for each field/type

