Structures in C

Heterogeneous data

Is placing these values in three different arrays convenient, logical,
or efficient?

Structure

The C struct constructor allows the definition of a
heterogeneous data structures with fields or members
of different types.
The name of each field within a structure defintion must be unique,
but often different structure defintions will share a common name.

Effectively every struct definition
introduces a new programmer-defined type.
Many programmers use typedef's for structures.

typedef struct state State ;
State NC ;

The FILE “type” used for file I/O operations in C
is typically defined with a typedef of a C structure.
The quotes are used because
technically a typedef is not a new type, but rather
a “synonym” for an existing type.

By the way, the C structure is rather like a method-less class
in either C++ or Java.

The dot operator

The dot operator joins a structure expression with a
field or member name.

The . operator has the highest precedence level and has
left-to-right precedence. Other operators at this level are:

Function call with ( )

Array subscript with [ ]

Field selection from object .

Field selection from pointer ->

Postfix ++

Postfix --

A horrendous example of this would be
f("United States")[30].Popluation++ used as a expression;
however, you do often find long sequences such as
USA[12].Name[0].

Structures and symbols

Consider each structure as having its own symbol table.
Here's a possible one for a 32-bit Intel architecture.

field

struct

offset

size

Abbrev

state

4

2

Name

state

0

4

Population

state

8

4

The adress of NC.Population would be the
address of NC plus the offset of Population.

In C you are allowed to assume that fields will be allocated in
memory in the same order as they appear in the definition; however,
because the compiler is free to insert padding between fields,
this assumption is rarely useful.

Structures as variables

In ANSI C, one structure can be assigned (with =) to another
and structures can be passed to and returned from functions.
This involves copying the entire structure and is reasonable only with
very small structures.

In C you can't return an
array from a function, but you can return a structure containing an array.

Initialization of structures

struct state NC = { "North Carolina", {'N','C'}, 9535483 } ;

Effectively every struct definition
introduces a new programmer-defined type.
Many programmers use typedef's for structures.

typedef struct state State ;
State NC ;

The FILE “type” used for file I/O operations in C
is typically defined with a typedef of a C structure.
The quotes are used because
technically a typedef is not a new type, but rather
a “synonym” for an existing type.

By the way, the C structure is rather like a method-less class
in either C++ or Java.

Dynamic allocation of structures using pointers

Of course, you do need to use a pointer to refer to fields within
dynamically allocated structures. For example,
you'd need to use (*pNC).Population
to access the Population field of our dynamic state.
Because dynamic structures are so popular, C has a special
syntax for refering to their fields:
P->F is a
way of saying (*P).F.
(Think of -> as a sign pointing in a direction.)
This allows you to refer to our state's population as
pNC->Population

Recursive structure definitions

It only gets worse. Structures are often defined to contain pointers to
other stuctures, and some of those structures may even be of the same
type!
Often this is quite natural. After all a structure representing a
person will need a reference to another person, such as a mother!