Page 86 - Programming Microcontrollers in C
P. 86
Pointers 71
We now know that the string “Microcontrollers run the
world!\n” is nothing more than a character array terminated by a
zero. The compiler creates that array in the memory of the program.
That array is not passed to printf. A pointer to that array is passed
to printf. Then printf prints the characters to the standard out
put and increments through the array until it finds the 0 terminator.
Then it quits. In other instances, arguments like s[] were used. In
these cases, C automatically knows to pass a pointer to the array. It is
interesting. If you have an array int s[nn] and pass that array to a
function as s, you can use an argument int* p in the function and
then in the function body deal with the array p[]. The C language
is extremely flexible in the use of pointers. An example of this type
of operation is as follows:
/* Count the characters in a string */
#include <stdio.h>
int strlen(char *);
int main(void)
{
int n,s[80];
fgets(s,80,stdin);
n=strlen(s);
printf(“The string length = %d\n”,n);
return 0;
}
int strlen( char *p)
{
int i=0;
while(p[i]!=0)
i++;
return i;
}