Character Arrays


You can have an array of any data type: integer, float, double etc. You can even have character arrays which can store strings. A string is an array of characters terminated by the null character (‘\0’). For example, if we store the word "hello" in a character array, the individual characters of "hello" will be stored and the last character of the array will be a null character (‘\0’). It is the null character that denotes the end of the string (this is very important when doing string operations –without the null character we will not be able to identify the end of the string in memory). The character data type can store only a single character but a character array can take in a consecutive series of characters depending on the size of the character array.

Example:

char word[6]={'h','e','l','l','o','\0'};

You have to provide space to accommodate the null character. You can also assign a string constant directly as below:

char word[6]="Hello";

"Hello" is called a string constant and the compiler will automatically add the null character to the array.

The following declaration:

char word[6]="Helloo"; //ERROR

is incorrect and will produce a compile error because there is no space for adding the null character. But if you don’t specify the size of the array then it is acceptable:

char word[ ]="Helloo";

In this case while compiling the compiler will set the size of the array and provide space for the null character also.

Beware: You can assign a string literal to a character array only at the time of declaration. You cannot use:

word[6]="Helloo"; //WRONG

Another Example:

char name[10];

The above declaration declares an array of character type that can store 10 characters. The name of the array is ‘name’.

// A program for getting the name of a person and displaying the name
# include <iostream.h>
int main ( )
{
char name[15];
cout<< "Enter your name : ";
cin>> name;
cout<< "Your name is "<<name;
return 0;
}

Remember: Whenever you create character array type strings then ensure that you have space to accommodate the null character.

Beware of Character Arrays (and the compiler in general):

Try out the following program in your compiler. The results may surprise you.

#include <iostream.h>
int main ( )
{
char name[20];
cout<<"Enter your full name : ";
cin>>name;
cout<<"You entered your full name as : "<<name;
return 0;
}

Run it and type your full name. What do you think the compiler will display?

Let's suppose the user types his full name as John Davis. ‘name’ is a character array that can store a maximum of 20 characters. John Davis has 10 characters (including the space between first and last name). So this is less than 20 characters. It seems like there is no problem but if you observe the output, it would be as follows:

You entered your full name as: John

The Davis is left out. Why? After the word ‘John’ a blank space has been left. When a blank space is encountered in the input (John Davis), the program will not read the line further on. Hence only the word John is stored in the array name.

So be careful. This isn't only for character arrays. This applies even when you want to get input for a number. If the user types 1 followed by a space and then 2, the value of the integer variable will be 1. You can try out variations in your compiler, so that you know exactly how this works.

You might be wondering whether there is a way to store the blank spaces? Yes, there is but you shouldn’t make use of the cin method of obtaining input from the user. The cin will stop when it sees a blank space but there are other ways which will store the blank space (those methods will terminate when they encounter a new line; a new line means that the user pressed the ‘enter’ key).

For the time being if you want to get the full name of a person just make use of two character arrays; one for the first name and one for the last name.

If you are really keen on using a single character array for storing white spaces as well, then you could make use of the gets( ) function.

int main( )
{
char name[80];
cout<<endl<<"Enter your full name: ";
gets(name);
cout<<name;
return 0;
}

In this program, you can enter a name with blank spaces and that will also be stored in the array ‘name’. The gets( ) function will terminate a character array input when the enter key is pressed (in other words, when it encounters a new-line character it will terminate).

Beware: gets( ) does not perform any array limit checking (also known as boundary checking). In case the array size is defined to be 5 and if the user types 10 characters, then it will cause an error.

Alternative Methods to store blank spaces and new line:

The function gets( ) does not perform any array dimension check and hence it doesn’t restrict the user in the number of characters entered. The result is that your program will terminate due to some run-time errors if the array size is exceeded. To avoid this problem there is another alternative function that you can use. This function is a member function and can be called using ‘cin’. (You’ll learn more about objects and member functions later). The function is called get( ).

#include <iostream.h>
int main( )
{
char name[10];
cout<<"Enter the name: ";
cin.get(name,10);
cout<<endl<<name;
return 0;
}

The output will be:

Enter the name: Ian Smith
Ian Smith

As you can see, the blank space is stored in the character array ‘name’. Let’s try a longer name:

Enter the name: Gregory Ian
Gregory I

"Gregory Ian" has more than 10 characters in it. The get( ) function will terminate the array after 9 characters (since the 10th character has to be the null character). Thus in this case we haven’t run into any run-time errors even though the user entered more characters.

The get( ) function has two parameters (the syntax has been simplified for easy understanding):

get(char-array, length-of-string)

You might not want a character array to be terminated by a new line (you may want to preserve the new line as well). In this case you can make use of:

get(char-array, length-of-string, ‘character-for-termination’)

You can tell the compiler as to when you want the array to be terminated (i.e. you can specify which character will terminate the array), by specifying the character in the field ‘character-for-termination’.

See the example below:

#include <iostream.h>
int main( )
{
char name[80];
cout<<"Enter the name: ";
cin.get(name,80,'*');
cout<<endl<<"The name stored is: "<<endl<<name;
return 0;
}

The output will be:

Enter the name: John
Ian Smith
Alex Shone*Luis
The name stored is:
John
Ian Smith
Alex Shone

If you observe the output, you will notice that the newline character has been preserved. The input was terminated once the program encountered an ‘*’ (which was specified as the character-for-termination).


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.