Part IV. Windows Programming Foundations

Chapter 9 - Arrays

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Output and Input of Strings
While C and C++ do supply the data type char, they do not have a data type for character strings. Instead, the programmer must represent a string as an array of characters. The array uses one cell for each character in the string, with the final cell holding the null character \0.
The next example shows how you can represent the three major types of transportation as a character string. The array szmode1 is initialized character by character by use of the assignment operator, the array szmode2 is initialized by use of the function scanf( ), and the array szmode3 is initialized in the following definition.
/*
*   string.c
*   This C program demonstrates the use of strings
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

main( )
{
 char        szmode1[4],             /
* car   */
             szmode2[6];             /
* plane */
 static char szmode3[5] = “ship”;    /
* ship  */

 szmode1[0] = ‘c’;
 szmode1[1] = ‘a’;
 szmode1[2] = ‘r’;
 szmode1[3] = ‘\0’;

 printf(“\n\n\tPlease enter the mode —> plane ”);
 scanf(“%s”,szmode2);

 printf(“%s\n”,szmode1);
 printf(“%s\n”,szmode2);
 printf(“%s\n”,szmode3);

 return(0);
}
The next definitions show how C treats character strings as arrays of characters:
char   szmode1[4],                  /* car   */
      szmode2[6];                  /
* plane */
static char szmode3[5] = “ship”;    /
* ship  */
Even though the szmode1 “car” has three characters, the array szmode1 has four cells—one cell for each letter in the mode “car” and one for the null character. Remember, \0 counts as one character. Similarly, the mode “plane” has five characters (“ship” has four) but requires six storage cells (five for szmode3), including the null character. Remember, you could also have initialized the szmode3[5] array of characters by using braces:
static char szmode3[5] = {‘s’,’h’,’i’,’p’,’\0’};
When you use double quotes to list the initial values of the character array, the system will automatically add the null terminator \0. Also, remember that the same line could have been written like this:
static char szmode3[] = “ship”;
This uses an unsized array. Of course, you could have chosen the tedious approach to initializing an array of characters that was done with szmode1. A more common approach is to use the scanf( ) function to read the string directly into the array as was done with szmode2. The scanf( ) function uses a %s conversion specification. This causes the function to skip white space (blanks, tabs, and carriage returns) and then to read into the character array szmode2 all characters up to the next white space. The system will then automatically add a null terminator. Remember, the array’s dimension must be large enough to hold the string along with a null terminator. Look at this statement one more time:
scanf(“%s”,szmode2);
Are you bothered by the fact that szmode2 was not preceded by the address operator &? While it is true that scanf( ) was written to expect the address of a variable, as it turns out, an array’s name, unlike simple variable names, is an address expression—the address of the first element in the array.
When you use the printf( ) function in conjunction with a %s, the function is expecting the corresponding argument to be the address of some character string. The string is printed up to but not including the null character.
The following listing illustrates these principles by using an equivalent C++ algorithm:
//
//  string.cpp
//  This C++ program demonstrates the use of strings
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>

main( )
{
 char        szmode1[4],                // car
             szmode2[6];                // plane
 static char szmode3[5] = “ship”;       // ship

 szmode1[0] = ‘c’;
 szmode1[1] = ‘a’;
 szmode1[2] = ‘r’;
 szmode1[3] = ‘\0’;

 cout << “\n\n\tPlease enter the mode —> plane ”;
 cin >> szmode2;

 cout << szmode1 << “\n”;
 cout << szmode2 << “\n”;
 cout << szmode3 << “\n”;

 return(0);
}
The output from the program looks like this:
car
plane
ship

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net