Previous: 7.3 Arrays, Pointers, Pointer Arithmetic
Up: 7 Arrays
Next: 7.5 Array Initializers
Previous Page: 7.3.2 Array Names vs Pointer Variables
Next Page: 7.5 Array Initializers

7.4 String Assignment and I/O

As we have seen, a character string in C is an array of characters with a terminating NULL character. Access to a character string requires only a pointer to the character array containing the characters. It is common to use the term, string, to loosely refer to either an array of characters holding the string, or to a character pointer that may be used to access the string; it should be clear from context which is meant.

When a character string constant is used in a program, the compiler automatically allocates an array of characters, stores the string in the array, appends the NULL character, and replaces the string constant by the value of a pointer to the string. Therefore, the value of a string constant is the value of a pointer to the string. We can use string constants in expressions just as we can use the names of arrays. Here is an example:

char *mp, msg[SIZE];

mp = "This is a message\n";

The compiler replaces the string constant by a pointer to a corresponding string. Since mp is a character pointer variable, we can assign a value of a fixed string pointer to mp. If necessary we can traverse and print the string using this pointer. On the other hand, since msg[] is declared as a character array, we cannot make the following assignment:
msg = "This is a message\n";       /* ERROR */
since we are attempting to modify a constant pointer, msg.

A string constant is just another string appropriately initialized and accessed by a pointer to it. We will therefore make no distinctions between strings and string constants; they are both strings referenced by string pointers. While strings and string constants are both strings, the contents of string constants cannot be changed in ANSI C.

We have been using string constants as format strings for printf() and in scanf(), which expect their first argument to be a string pointer; i.e. a char pointer. The compiler has automatically created an appropriate string and replaced the string by a string pointer. Instead of writing a format string directly in a function call, we could pass a string pointer that points to a format string. Here is an example:

char *mesg;
     int n;

n = 1; mesg = "This is message number %d\n"; printf(mesg, n);

The string constant is stored by the compiler somewhere in memory as an array of characters with an appended NULL character. A pointer to this character array is assigned to the character pointer variable, mesg. The function printf() then uses the pointer to retrieve the format string, and print the string:

The functions, printf() and scanf() can be used for string input and output as well. Array names or properly initialized pointers to strings must be passed as arguments in both cases. The conversion specification for strings is %s. For example, consider the task of reading strings and writing them out. Here is an example program.

/*  File: fcopy.c
    This program reads strings from standard input using scanf() and writes
    them to standard output using printf().
*/
#include <stdio.h>
#include "araydef.h"
main()
{   char mesg[SIZE];

printf("***Strings: Formatted I/O***\n\n"); printf("Type characters, EOF to quit\n"); while (scanf("%s", mesg) != EOF) printf("%s\n", mesg); }

Sample Session:

The conversion specification, %s indicates a string and the corresponding matching argument must be a char pointer. When scanf() reads a string it stores it at the location pointed to by mesg --- note we do not use &mesg since mesg is already a pointer to an array of characters. Then, printf() prints the string pointed to by mesg. When scanf() reads a string using %s, it behaves like it does for numeric input, skipping over leading white space, and reading the string of characters until a white space is reached. Thus, scanf() can read only single words, storing the string of characters read into an array pointed to by the argument, mesg and appending a NULL character to mark the end of the string. On the other hand, printf() prints the string pointed to by its argument, mesg, printing the entire string (including any white space) until a NULL character is reached. The sample session shows that each time scanf() reads a string, only a single word is read from the input line and then printed.

As we said, when scanf() reads a string, the string argument must be a pointer that points to an array where the input characters are to be stored. For example, here are correct and incorrect ways of using scanf():

char * mp, * mptr, msg[SIZE];

scanf("%s", mp); /* BUG */ scanf("%s", msg); /* OK */ mptr = msg; scanf("%s", mptr); /* OK */

The first scanf() is incorrect because mp has not been initialized and, therefore, does not point to an array where a string is to be stored. The other statements are correct; in each case, the pointer points to an array.



Previous: 7.3 Arrays, Pointers, Pointer Arithmetic
Up: 7 Arrays
Next: 7.5 Array Initializers
Previous Page: 7.3.2 Array Names vs Pointer Variables
Next Page: 7.5 Array Initializers

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:56:22 HST 1994