Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C Programming for microcontrollers (Joe Pardue, 2005).pdf
Скачиваний:
260
Добавлен:
12.08.2013
Размер:
4.55 Mб
Скачать

Chapter 10: C Structures

Structure Arrays

In the last section we used:

struct pwm pulser1k50; struct pwm pulser1k25; struct pwm pulser4k10;

pulser1k50 = makePWM(1000,128);//make a 50% duty 1000 kHz pulse pulser1k25 = makePWM(1000,64);//make a 25% duty 1000 kHz pulse pulser4k10 = makePWM(4000,25);//make a 10% duty 4000 kHz pulse

We could have defined an array of these structures and made them as follows

struct pwm pulser[] = {

{1000, 128 };

{1000, 64 };

{4000, 25);

}

Actually the prior, non-array version probably makes more sense because the instance names carry more user information. pulser1k25 versus pulser[1]. But there are cases where arrays of structures come in real handy.

Typedef

C allows us to create new data type names with the typedef facility.

typedef unsigned char Byte;

would cause the compiler to handle anything declared as Byte as if it was an unsigned char. Only actual C types can be aliased in this manner. Typedef works somewhat like define, in that it provides an alias, but defines are handled by the preprocessor and more limited in what they can do.

Typedefs are useful in making software more readable: Byte makes more sense in our use than unsigned char. Another use is to facilitate portability of software by putting machine specific types in typedefs so you can change them as you change machines.

246