next up previous
Next: Multi-file programs Up: Scientific programming in C Previous: Arrays

Character strings

The basic C character data type is called char. A character string in C can be represented symbolically as an array of data type char. For instance, the declaration
char word[20] = "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[0] = 'f'  word[1] = 'o'  word[2] = 'u'  word[3] =  'r'  word[4] = '\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. Hence, word[i] and *(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[20] =  "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[20] =  "four";
. . .
word[0] = 'f';
word[1] = 'i';
word[2] = 'v';
word[3] = 'e';
word[4] = '\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.


next up previous
Next: Multi-file programs Up: Scientific programming in C Previous: Arrays
Richard Fitzpatrick 2006-03-29