int numList[10]; // declare array of ints. numList is a pointer to an int cout << numList; // outputs the address stored in "numList"
However, on character arrays only, we can do this and the output will be the contents of the array (this only works on strings!)
char name[10] = "Marvin"; cout << name; // the output will be the word Marvin (not the address)
The << operator knows when to stop because strings are null terminated (this operator will output characters until the '\0' characer, which has ASCII value 0, is encountered). For input, we would like to read in strings with the >> operator, but how would we know when to stop? You can't type a null character on the keyboard! The >> operator, when the right side operand is the name of a string (i.e. type char* ) is defined to halt on "white space" (like spaces, tabs, newlines) after reading in non-white-space characters. (It will still ignore any leading white space, as with other types of input using >> as well).
char greeting[15]; cin >> greeting; // allows input of a string, stored in the greeting array
In the above code, if the user types "Hello", then the string "Hello" is stored. However, if the user types "Hello World", then the string "Hello" is stored, because >> stops on white space (the space between Hello and world). If we want to allow the white space to be read, as well, we can use one of the following functions, which are members of the istream class:
get(char* string, int length, char delimiter = '\n'); getline(char* string, int length, char delimiter = '\n');
Both of these functions allow the passing of 3 parameters (or, optionally, 2 parameters, since the third has a default value). The first parameter is the name of the character array (a pointer-to-char). The second parameter should be the length of the array (how big a string do you have room for?). The third parameter is a delimiter, which is the character at which input will stop reading. This defaults to the newline character by default if the third parameter is not provided
Both of these functions will extract data from the input stream and store it in the string, like the >> operator, except that they will read up to length number of characters, until the delimiter is seen. (So, these will not stop at all white space.) The difference between this get function and the getline function is that get will leave the delimiter on the input stream (where it will be seen by the next input extraction), while getline throws the delimiter away. All of these input functions (>>, get, and getline) will automatically add the null terminator '\0' at the end of the string that was read. Examples:
char greeting[15], name[10], other[20]; cin.getline(greeting,15); // gets input into the greeting array cin.get(name,10,'.'); // gets input into the name array cin.getline(other,20); // gets input into the other array
Suppose that the data on the input stream (i.e. typed onto the keyboard, for instance) is:
Hello, World Joe Smith. He says hello.At this point, the contents of each string are:
greeting: "Hello, World" name: "Joe Smith" other: ". He says hello."
#include <string.h>or in the newer library style:
#include <cstring>
This string library contains many useful string manipulation functions. A few of the more commonly used ones are mentioned here. (The textbook contains more detail on these functions in chapter 5).
Here is an example of the prototype of one string function:
char* strcpy( char * s1, const char * s2 );
This function copies the contents of the second string (s2) into the first (s1). Note that both parameters use Pass By Address, and the object is that two strings are passed in by name. Since strings are character arrays, we are really passing the address of each string (char array) into the function, and not the entire string itself. Since the addresses will be stored in the local parameters s1 and s2, the function has access to the original strings. Note also that the second parameter is declared const -- this ensures that the string being copied is not modified by the function. The first string must be modified, because this is where we are copying to.
The definitions of these functions are easily written with pointer and
array techniques that we have already seen.
This link shows some possible ways of
defining some of these functions.