Getting Started

(lily) #1

Chapter 10: C Structures


of

type, and like other data types variables
an be declared to be of that type:

int x, y, z;

r3;

hich eates struct pwm.

his ‘d clarati tantiation’ of a structure is an important concept that
n of pwm did not
ave a following it, so it exists as a prototype and no memory is
llocate re we added the variables, pulser1,pulser2,
of the structure in
memory. Not only is instantiation important word in the object oriented
ming y conversation.
Hey b e, wa e our procreative potential?”


e can instantiate our struct and assign data to it:

struct pwm pulser1 = { 1000, 127};

ember

We access m bers of structs using the structure member operator ‘.’:


int x,y;


ls 1000

between pulseFreq and pwm.pulseFreq. As we’ll see in a minute, this reuse
names, normally a no-no, can help clarify code.


The structure declaration creates a data
c


struct { .... } x, y, z;


Usually you see this done as:


struct pwm {
int pulseFreq;
unsigned char pulseWidth;
}pulser1,pulser2,pulse


w cr three instances, pulser1,pulser2,pulser3, of the


T e on versus ins
you’ll see a lot if you move on up to C++. The first declaratio
h variable list
a d. In the second version, whe
and pulser3, we actually create three copies (instances)


program world, it’s very geeky to find uses for it in ordinar
“ ab nna instantiat


W


which defines a pulse with a frequency of 1 kHz and a 50% duty cycle (rem



  • 127 is half of 255 which is 100%).


em

x = pulser1.pulseFreq; // x now equa

Free download pdf