SMILEY’S WORKSHOP ☺
that is, I started a count with 1 instead of 0, and wrote
the last data byte to an address that was one location
outside of the video buffer. That byte was only
occasionally important enough to crash the system and I
nearly went nuts trying to figure out what was wrong.
When your device crashes intermittently with no apparent
rhyme or reason, you may well be suffering from a bad
We declare a variable to be a pointer by preceding its
name with an *, called the indirection or dereferencing
char *q; // q is a pointer to a char
We get the address of a variable using &, called the
address of operator:
// create a character variable initialized
// to 0x48
char v = ‘H’;
■ FIGURE 2. Peles C New Project.
// put the address of v in the pointer q
q = &v;
In the case of the AVR, the compiler will know to
create a 16-bit storage location for the ‘*q’ variable just
like it knows to create an eight-bit location for the ‘v’
variable. Now, take a deep breath and commit all that to
your memory so that the next time you see something like
*myOhmy, you’ll know that it is the address of something
— a pointer — and if you see &myAmi you’ll know that this
is an operation that yields the address of the variable
myAmi. Then, the statement myOhmy = &myAmi will
make perfect sense.
C Programming Examples
To help our understanding, let’s play with these
concepts on a PC using a simple and free C compiler.
We’ll switch over to
AVRs next month.
Let’s use Pelles C
that you can
Interestingly, the Pelles C has a wizard application that
creates a version of this program as a template for writing
Open Pelles C, click on the File menu, and select
New\Project. Figure 2 shows the resulting window with
‘Console Application Wizard’ highlighted and Hello_World
typed into the ‘Name:’ field.
Click OK and you’ll see the window shown in Figure
3. Check the ‘A “Hello, world” program. Yes, the Hello
World program is so basic that it is included for you!
Click Next and you’ll see the window in Figure 4.
Now click the finish button. As if by magic, Pelles will
■ FIGURE 3. Pelles C Console Wizard Part 1.
■ FIGURE 4. Console Wizard Part 2.
This is canonic
since it comes from
the C bible
(K&R p. 6).
June 2010 69