Structs and Members
A Struct is a data structure in memory, consisting of one top node and zero or more sub-nodes called members. Each member can hold a scalar data value (such as a number, string, or date), or it can hold another (nested) Struct.
For example, a book is a complex data structure that can be represented by a Struct.
This illustrates a number of characteristics of Structs:
- A Struct consists of a top-level node and an
arbitrary number of sub-nodes, known as members.
For example, the
Book
Struct contains members calledTitle
,Preface
, andChapter
. - A Struct member can have a value. This can be:
- A scalar value, such as
numeric
,float
,string
,boolean
,date
, ortime
. For example, the value of firstTitle
member is the string"Getting Started"
.A Struct member that has no value, or only a single scalar value, is a Struct leaf.
- A Struct, in which case it is a nested
Struct. For example, each
Chapter
is a nested Struct because it has child members.
- A scalar value, such as
- A Struct can hold a mixture of both scalar
members and nested Struct members.
For example, the
Book
Struct contains two scalar members (Title
andPreface
) and two nested Structs (Chapter
). - A Struct or Struct member can have a name.
If it has a name, the name is case-sensitive. If it is a nested Struct, it is known to its parent by its name. The name can consist of an empty string (
""
), so an expression such asvStruct->""
refers to all members that have an empty string as a name. - Members of a Struct can have the
same name.
For example, there are multiple members called
Chapter
andSection
. Thus, if a struct variable references a Struct by name (for examplevBook->Chapter
), it contains a collection of references to all Structs with that name. For more information, see Struct Dereference Operator (->). - Struct members are sequentially ordered.
They can be addressed by their index position, making it possible to address a specific member, even if it has the same name as another member, or no name. For example,
vBook->Chapter{2}
refers to the second chapter member. For more information, see Struct Index Operator ({N}). - A Struct or nested Struct node can have a
special
$tags
Struct containing annotations. This Struct is not counted as a normal member of the Struct. In the example,$tags
is available (but not shown) on the Book Struct, and on the Chapter Structs. For more information, see Struct Annotations.
Note: The term Struct node is often used
for the top-level Struct and nested Structs, to distinguish them from scalar Struct members. The
term refers to the Struct, excluding child nodes but including its properties, such as
name
and $tags
.
Most importantly, Structs are dynamic. You can add members to Structs, copy or move members from one Struct to another, and change the values of Struct members. For more information, see Working with Structs.
When working with Structs for XML or XHTML, you may need to add members to Struct leaves, in which case they implicitly become nested Structs with mixed content, or remove members from a nested Structs to make them Struct leaves.For more information, see Struct Leaves.