11.16Unions. 4tH 4tH Forth compiler
Anúncio
Anúncio
CHAPTER 11. ADVANCED PROGRAMMING 151
You now got two different structures, ”/Person” and ”/Employee”, that share the first four fields. Defining a structure within a structure is possible too:
\ create structure struct
32 +field Firstname
64 +field Lastname struct
64 +field Address
8 +field Zip
32 +field City
+field Location
4 +field Age end-struct /Person
\ structure within a structure
\ let’s give it a name
Let’s use it:
/Person string Person \ allocate some space
\ initialize it s" Hans" s" Bezemer"
Person -> Firstname place
Person -> Lastname place s" Lagendijk 79" Person -> Location -> Address place s" Den Helder" Person -> Location -> City place s" 44" Person -> Age place
And finally, if you want to define a structure and allocate the memory it occupies at the same time, you can do that one too. After all, ’+FIELD’ is literal expression
5
: struct
32 +field Firstname
64 +field Lastname
8 +field Initials string Person
\ define the structure
\ allocate the structure s" Hans" Person -> Firstname place s" JL" Person -> Initials place
Well, if that isn’t a complete implementation, I don’t know what is..
11.16
Unions
A union is a bunch of variables that share the same memory. Defining it in 4tH is quite easy: include lib/ncoding.4th
struct
32 /field nell /field end-struct /union
\ include NCODING library
\ start defining the union
\ first field
\ second field
\ end union definition
/union string myunion \ allocate some space
\ now use it s" Hans Bezemer" myunion place
1960 myunion n!
5
See section 7.13.
Download
Anúncio