char word = "four";initializes the 20-element character array word, and then stores the character string ``four'' in it. The resulting elements of word are as follows:
word = 'f' word = 'o' word = 'u' word = 'r' word = '\0'with the remaining elements undefined. Here, 'f' represents the character ``f'', etc., and
'\0'represents the so-called null character (ASCII code 0), which is used in C to signal the termination of a character string. The null character is automatically added to the end of any character string enclosed in double quotes. Note that, since all character strings in C must be terminated by the (invisible) null character, it takes a character array of size at least n+1 to store an n-letter string.
As with arrays of numbers, the name of a character array is essentially equivalent to
a pointer to the first element of that array.
*(word + i) both refer to the
same character in the character array word. Note, however, that the name of
a character array is not a true pointer, since the address to which it points
cannot be changed. Of course, we can always represent a character array using
a true pointer. Consider the declaration
char *word = "four";Here, word is declared to be a pointer to a char which points towards the first element of the character string
'f' 'o' 'u' 'r' '\0'. Unlike the name of a character array, a true pointer to a char can be redirected. Thus,
char *word = "four"; . . . word = "five";is legal, whereas
char word = "four"; . . . word = "five";is illegal. Note that, in the former example, the addresses of the first elements of the strings ``four'' and ``five'' are probably different. Of course, the contents of a character array can always be changed, element by element--it is just the address of the first element which must remain constant. Thus,
char word = "four"; . . . word = 'f'; word = 'i'; word = 'v'; word = 'e'; word = '\0';is perfectly legal.
Note, finally, that a character string can be printed via the printf()
function by making use of a
%s entry in its control string: e.g.,
printf("word = %s\n", word);Here, the second argument, word, can either be the name of a character array or a true pointer to the first element of a character string.