Nested structure

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Psych
Enthusiast
Enthusiast
Posts: 239
Joined: Thu Dec 18, 2008 3:35 pm
Location: Wales, UK

Nested structure

Post by Psych »

In C you are able to declare a structure union with intelligently defined widths, making byte alignment automatic, however I found no easy way to achieve this in PB unless I define the word pairs in their own named structure

Could we have this?

Code: Select all

Structure WordPair
	StructureUnion
		Structure
			LoWord.w
			HiWord.w
		EndStructure
		Long.l
	EndStructureUnion
EndStructure
Enabling us to access each field with having to qualify the substructure.
----------------------------------------------------------------------------
Commenting your own code is admitting you don't understand it.
----------------------------------------------------------------------------
User avatar
STARGÅTE
Addict
Addict
Posts: 2232
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Nested structure

Post by STARGÅTE »

Methode 1:

Code: Select all

Structure LongFields
	Lo.w
	Hi.w
EndStructure

Structure WordPair
	StructureUnion
		LongFields.LongFields
		Long.l
	EndStructureUnion
EndStructure

Define WordPair.WordPair
WordPair\Long = $FF0055AA

Debug Hex(WordPair\LongFields\Lo, #PB_Word)
Debug Hex(WordPair\LongFields\Hi, #PB_Word)
Methode 2:

Code: Select all

Structure WordPair
	StructureUnion
		LongFields.w[0]
		Long.l
	EndStructureUnion
EndStructure

Define WordPair.WordPair
WordPair\Long = $FF0055AA

Debug Hex(WordPair\LongFields[0], #PB_Word)
Debug Hex(WordPair\LongFields[1], #PB_Word)
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 moreTypeface - Sprite-based font include/module
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Nested structure

Post by Demivec »

Post Reply