Language > Data Types > Creating and Defining Structures

# Creating and Defining Structures

A named structure is created by executing a structure-definition expression, which is an expression of the following form:

{Structure_Name, Tag_Name1 : Tag_Definition1, ..., Tag_Namen : Tag_Definitionn}

Anonymous structures are created in the same way, but with the structure’s name omitted.

{Tag_Name1 : Tag_Definition1 , ..., Tag_Namen : Tag_Definitionn}

Anonymous structures can also be created and combined using the CREATE_STRUCT function.

Tag names may not be IDL Reserved Words, and must be unique within a given structure, although the same tag name can be used in more than one structure. Structure names and tag names follow the rules of IDL identifiers: they must begin with a letter; following characters can be letters, digits, or the underscore or dollar sign characters; and case is ignored.

As mentioned above, each tag definition is a constant, variable, or expression whose structure defines the structure and initial value of the field. The result of the structure definition expression is an instance of the structure, with each field set equal to its tag definition.

A named structure that has already been defined can be referred to by simply enclosing the structure’s name in braces, as shown below:

{Structure_Name }

The result of this expression is a structure of the designated name.

Note: When a new instance of a structure is created from an existing named structure, all of the fields in the newly-created structure are zeroed. This means that fields containing numeric values will contain zeros, fields containing string values will contain empty strings, and fields containing pointers or objects will contain null pointers or null objects. In other words, no matter what data the original structure contained, the new structure will contain only a template for that type of data.

Also, when making a named structure that has already been defined, the tag names need not be present:

{Structure_Name, expression1, ..., expressionn}

All of the expressions must agree in structure with the original tag definition.

Once defined, a given named structure type cannot be changed. If a structure definition with tag names is executed and the structure already exists, each tag name and the structure of each tag field must agree with the original definition. Anonymous structures do not have this restriction because each instance has its own definition.

## Structure Inheritance

Structures can inherit tag names and definitions from other structures. To cause one structure to inherit tags from another, use the INHERITS specifier. For example, if we define a structure one as follows:

A = {one, data1a:0, data1b:0L }

we can define a second structure two that includes the tags from the one structure with the following definition statement:

B = { two, INHERITS one, data2:0.0 }

This is the same as defining the structure two with the statement:

B = { two, data1a:0, data1b:0L, data2:0.0 }

Note that the fields of the one structure are included in the two structure in the position that the INHERITS specifier appears in the structure definition.

Remember that tag names must be unique. If you use structure inheritance, be sure that the tag names in the inherited structure do not conflict with the tag names in the inheriting structure.

Structures that are inherited must be defined before the inheriting structure can be defined. If a structure inherits tags from another structure that is not yet defined, IDL will search for a routine to define the inherited structure. If the inherited structure cannot be defined, definition of the new structure fails.

While structure inheritance can be used with any structure, it is most useful when dealing with object class structures. When the INHERITS specifier is used in a class structure definition, it has the added effect of defining the inheriting object as a subclass of the inherited class.

## Example of Creating a Structure

Assume that a star catalog is to be processed. Each entry for a star contains the following information: star name, right ascension, declination, and an intensity measured each month over the last 12 months. A structure for this information is defined with the following IDL statement:

A = {star, name:'', ra:0.0, dec:0.0, inten:FLTARR(12)}

This structure definition is the basis for all examples in this chapter. The statement above defines a structure type named star, which contains four fields. The tag names are name, ra, dec, and inten. The first field, with the tag name, contains a scalar string as given by its tag definition. The following two fields each contain floating-point scalars. The fourth field, inten, contains a 12-element, floating-point array. Note that the type of the constants, 0.0, is floating point. If the constants had been written as 0, the fields ra and dec would contain short integers.

The same structure is created as an anonymous structure by the statement:

A = {name:'', ra:0.0, dec:0.0, inten:FLTARR(12)}

or by using the CREATE_STRUCT function:

A = CREATE_STRUCT('name', '', 'ra', 0.0, 'dec', 0.0, $

'inten', FLTARR(12))