HP C A.06.05 Reference Manual
Expressions and Operators
Pointer Operators (*, ->, &)
Chapter 5124
{
int count = 0;
while (*arg++)
count++;
return count;
}
int main(void)
{
char a_word[30];
int number_of_characters;
printf("Enter a word -- ");
scanf("%s", a_word);
number_of_characters = count_chars(a_word);
printf("%s contains %d characters.\n", a_word,
number_of_characters);
}
If you execute this program, you get the following output:
Enter a word -- Marilyn
Marilyn contains 7 characters.
Example 5-2 Accessing a 2-Dimensional Array Through Pointers
/* Program name is "pointer_array_example2". This program
* demonstrates two ways to access a 2-dimensional array. */
#include <stdio.h>
int main(void)
{
int count = 0, which_name;
char c1, c2;
static char str[5][10] = {"Phil", "Sandi", "Barry",
"David", "Amy"};
static char *pstr[5] = { str[0], str[1], str[2],
str[3], str[4]};
/* pstr is an array of pointers. Each element in the array
* points to the beginning of one of the arrays in str.
*/
/* Prompt for information. */
printf("Which name do you want to retrieve?\n");
printf("Enter 0 for the first name,\n");
printf(" 1 for the second name, etc. -- ");
scanf("%d", &which_name);
/* Print name directly through array. */
while (c1 = str[which_name][count++])