Programming in C

(Barry) #1

268 Chapter 11 Pointers


Initializing the textarray in this manner does not have the effect of storing a pointer to
the character string "This is okay."inside text,but rather the actual characters
themselves inside corresponding elements of the textarray.
If textis a character pointer, initializing textwith the statement
char *text = "This is okay.";
assigns to it a pointer to the character string "This is okay."
As another example of the distinction between character strings and character string
pointers, the following sets up an array called days, which contains pointersto the names
of the days of the week.
char *days[] =
{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday" };
The array daysis defined to contain seven entries, each a pointer to a character string.
So days[0]contains a pointer to the character string "Sunday",days[1]contains a
pointer to the string "Monday", and so on (see Figure 11.10).You could display the name
of the third weekday, for example, with the following statement:
printf ("%s\n", days[3]);

days[0]
days[1]
days[2]
days[3]
days[4]
days[5]
days[6]

S u n d ay\0

M o n d ay\0

T u e s d ay\0

W e d n e s d ay\0

T h u r s d ay\0

F r i d ay\0

S a t u r d ay\0

Figure 11.10 Array of pointers.

The Increment and Decrement Operators Revisited
Up to this point, whenever you used the increment or decrement operator, it was the
only operator that appeared in the expression.When you write the expression ++x,you
know that this has the effect of adding 1 to the value of the variable x. And as you have
Free download pdf