You can find the source code and supplements for this
article in Workshop23.zip at www.nutsvolts.com.
SMILEY’S WORKSHOP
for(i =0 ; *x != ‘\0’; i++)
{
putchar(*x);
x++;
}
}
In C, the name of an array without the
following square brackets is a pointer to the first
element in the array data sequence. For instance,
if the C compiler just happened to follow our
example and put the characters in the greet[] array
beginning at address 0x0400 as shown in Figure 1, then
the word ‘greet’ will be a pointer to address 0x0400. (Our
compiler might, theoretically, store that address at
addresses 0x0678 and 0x0679 also as shown in Figure 1.)
So, we define the consoleOut(char *) to tell the compiler
that this function takes a pointer to a character as a
parameter (char *). Then, when we use consoleOut(greet)
the compiler knows that ‘greet’ is a pointer to a char and
to use the address of the array named ‘greet’
(hypothetically 0x0400) and not any element of that array.
We send the specific ‘greet’ pointer in the parameter list,
but the function is generic so it receives the pointer as ‘*x’
and uses it as a pointer to the data in the greet array. So,
when the ‘for’ loop begins, *x is pointing to ‘H’, then we
increment the pointer. Notice that when we increment the
address we are using ‘x’ which the compiler knows is the
address, so x++ adds one to the
address.
Here’s one more example to
show a use of the ‘address-of’
operator &.
■ FIGURE 7. Console pointer demo.
Note that the ‘\’ at the end of the line allows the
printf() statements to continue on the next line which is
only necessary here to reduce the width of the source
code to make it fit the text. Also, don’t worry so much
about all the ‘stuff’ like 0x%X in the printf() statements
which are out of the scope for this discussion. Just pay
attention to the use of ‘*’ and ‘&’. Executing this code
yields the results shown in Figure 7.
Next month, we will apply some of this hard-won
knowledge to the AVR and give examples that run on
microcontrollers. Meanwhile, if you want to get a leg up
on C, buy the book C Programming for Microcontrollers
from Nuts & Volts, and if you want to get two legs up, get
the combo with the book and hardware projects kit. It will
come in handy. NV
;;;;;;;;;;;;;;;
#include <stdio.h>
int main(int argc, char
*argv[])
{
char c1 = ‘H’;
char c2 = ‘e’;
// Assign the address of c2
// to ptr
char *ptr = &c2;
;;;;;;
Optional LCD Module: $17.95
;;; ;;;
;;;; ;;;;;;;; ;;;; ;;;;;; ;;;;;; ;;;;;;;;;;
ASSEMBLED
printf(“\n”);
printf(“c1 has the hex value
0x%X (%c) and is stored at
%p\n”\
;;; ;;;
;;;;;;;;
;;;;;;;;;;;
,c1, c1,(void
*)&c1);
printf(“c2 has the hex value
0x%X (%c) and is stored at
%p\n”\
, c2, c2,(void
*)&c2);
printf(“ptr has the value %p
and is stored at %p\n”\
, ptr, (void
*)&ptr);
printf(“The value of the
char pointed to by ptr is
0x%X (%c)\n”\
, *ptr,*ptr);
return 0;
;;; ;;;
ASSEMBLED
;;;;; ;;;; ;;;; ;;;;;;;;;;; ;;;;; ;;;; ;;;;;;
Assembled: $49.95
;;; ;;;;;;;;;;;;; ; ;;;;;;; ; ;;;;;; ;;;;;;;;;; ; ;;;;;; ;;;;;;;;;;;; ; ; ; ; ;; ;;;;;;; ;;;;;;;
;;; ;;;
;;;;;;
;;;;;;; ;;;; ;;;;;; ;;;;;;;;;;; ;;;;;
;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;
;;;;;;
}
June 2010 71