Structures

A structure is a derived type usually representing a user-defined collection of named members (or components). These members can be of any type, either fundamental or derived (with some restrictions to be discussed later), in any sequence. In addition, a structure member can be a bit field.

Unlike arrays, structures are considered to be single objects. The mikroC PRO for ARM structure type lets you handle complex data structures almost as easily as single variables.

The mikroC PRO for ARM supports anonymous structures.

Structure Declaration and Initialization

Structures are declared using the keyword struct:

struct tag {member-declarator-list};

Here, tag is the name of a structure; member-declarator-list is a list of structure members, actually a list of variable declarations. Variables of structured type are declared the same as variables of any other type.

The member type cannot be the same as the struct type being currently declared. However, a member can be a pointer to the structure being declared, as in the following example:

struct mystruct {mystruct s;};   /* illegal! */
struct mystruct {mystruct *ps;}; /* OK */

Also, a structure can contain previously defined structure types when declaring an instance of declared structure. Here is an example:

/* Structure defining a dot: */
struct Dot {float x, y;};

/* Structure defining a circle: */
struct Circle {
  float r;
  struct Dot center;
} o1, o2;
/* declare variables o1 and o2 of Circle */

Note that the structure tag can be omitted, but then additional objects of this type cannot be declared elsewhere. For more information, see the Untagged Structures below.

Structure is initialized by assigning it a comma-delimited sequence of values within braces, similar to array. For example:

/* Referring to declarations from the example above: */

/* Declare and initialize dots p and q: */
struct Dot p = {1., 1.}, q = {3.7, -0.5};

/* Declare and initialize circle o1: */
struct Circle o1 = {1., {0., 0.}};  // radius is 1, center is at (0, 0)

Incomplete Declarations

Incomplete declarations are also known as forward declarations. A pointer to a structure type A can legally appear in the declaration of another structure B before A has been declared:

struct A;  // incomplete
struct B {struct A *pa;};
struct A {struct B *pb;};

The first appearance of A is called incomplete because there is no definition for it at that point. An incomplete declaration is allowed here, because the definition of B doesn’t need the size of A.

Untagged Structures and Typedefs

If the structure tag is omitted, an untagged structure is created. The untagged structures can be used to declare the identifiers in the comma-delimited member-declarator-list to be of the given structure type (or derived from it), but additional objects of this type cannot be declared elsewhere.

It is possible to create a typedef while declaring a structure, with or without tag:

/* With tag: */
typedef struct mystruct { ... } Mystruct;
Mystruct s, *ps, arrs[10];  /* same as struct mystruct s, etc. */

/* Without tag: */
typedef struct { ... } Mystruct;
Mystruct s, *ps, arrs[10];

Usually, there is no need to use both tag and typedef: either can be used in structure type declarations.

Untagged structure and union members are ignored during initialization.

Anonymous Structures

mikroC PRO for ARM allows you to declare a structure variable within another structure without giving it a name.
These nested structures are called anonymous structures.

You can access the members of an anonymous structure as if they were members in the containing structure :

struct phone{
  int  areacode;
  long number;
};

struct person {
  char   name[30];
  char   gender;
  int    age;
  int    weight;
  struct phone;    // Anonymous structure; no name needed
} Jim;

  Jim.number = 1234567; 
}
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code