Книга: Advanced PIC Microcontroller Projects in C

3.1.16 Structures

3.1.16 Structures

A structure can be used to collect related items that are then treated as a single object. Unlike an array, a structure can contain a mixture of data types. For example, a structure can store the personal details (name, surname, age, date of birth, etc.) of a student.

A structure is created by using the keyword struct, followed by a structure name and a list of member declarations. Optionally, variables of the same type as the structure can be declared at the end of the structure.

The following example declares a structure named Person:

struct Person {
 unsigned char name[20];
 unsigned char surname[20];
 unsigned char nationality[20];
 unsigned char age;
}

Declaring a structure does not occupy any space in memory; rather, the compiler creates a template describing the names and types of the data objects or member elements that will eventually be stored within such a structure variable. Only when variables of the same type as the structure are created do these variables occupy space in memory. We can declare variables of the same type as the structure by giving the name of the structure and the name of the variable. For example, two variables Me and You of type Person can be created by the statement:

struct Person Me, You;

Variables of type Person can also be created during the declaration of the structure as follows:

struct Person {
unsigned char name[20];
unsigned char surname[20];
unsigned char nationality[20];
unsigned char age;
} Me, You;

We can assign values to members of a structure by specifying the name of the structure, followed by a dot (“.”) and the name of the member. In the following example, the age of structure variable Me is set to 25, and variable M is assigned to the value of age in structure variable You:

Me.age = 25;
M = You.age;

Structure members can be initialized during the declaration of the structure. In the following example, the radius and height of structure Cylinder are initialized to 1.2 and 2.5 respectively:

struct Cylinder {
 float radius;
 float height;
} MyCylinder = {1.2, 2.5};

Values can also be set to members of a structure using pointers by defining the variable types as pointers. For example, if TheCylinder is defined as a pointer to structure Cylinder, then we can write:

struct Cylinder {
 float radius;
 float height;
} *TheCylinder;
TheCylinder->radius = 1.2;
TheCylinder->height = 2.5;

The size of a structure is the number of bytes contained within the structure. We can use the sizeof operator to get the size of a structure. Considering the above example,

sizeof(MyCylinder)

returns 8, since each float variable occupies 4 bytes in memory.

Bit fields can be defined using structures. With bit fields we can assign identifiers to bits of a variable. For example, to identify bits 0, 1, 2, and 3 of a variable as LowNibble and to identify the remaining 4 bits as HighNibble we can write:

struct {
 LowNibble  : 4;
 HighNibble : 4;
} MyVariable;

We can then access the nibbles of variable MyVariable as:

MyVariable.LowNibble = 12;
MyVariable.HighNibble = 8;

In C language we can use the typedef statements to create new types of variables. For example, a new structure data type named Reg can be created as follows:

typedef struct {
 unsigned char name[20];
 unsigned char surname[20];
 unsigned age;
} Reg;

Variables of type Reg can then be created in the same way other types of variables are created. In the following example, variables MyReg, Reg1, and Reg2 are created from data type Reg:

Reg MyReg, Reg1, Reg2;

The contents of one structure can be copied to another structure, provided that both structures are derived from the same template. In the following example, structure variables of the same type, P1 and P2, are created, and P2 is copied to P1:

struct Person {
 unsigned char name[20];
 unsigned char surname[20];
 unsigned int age;
 unsigned int height;
 unsigned weight;
}
struct Person P1, P2;
........................
........................
P2 = P1;

Оглавление книги


Генерация: 0.517. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз