Character Arrays in C++

The procedure for declaring character array is almost the same as for other data types such as integer or floating point. We can declare the character array by means of alphanumeric characters. The general format of the character array is,

storage_class character_data_type array_name[expression];

The storage_class is optional and it may be either one of the scope of the variable such as automatic, external, static, or register; the array name can be any valid C++ identifier and the expression a positive integer constant.

For example:

char page [40];
char sentence [300];
static char line[50];

Each element of the array is placed in a definite memory space and each element can be accessed separately. The array element should end with the null character as a reference for the termination of a character array.

Initialization of character array:

The following array definition creates a 13-element array with Greetings[O] being initialized to ‘H’, Greeting[l] being initialized to ‘e’, and so on through Greetings[11] being initialized to ‘d’ and Greetings[12] being initialized to ‘\0′.

char Greetings[] = "Hello, world";

The iostream library includes a definition of the insertion operator < for a right operand that is a char array. The operation displays all of the characters of the array that precede the first null character in the array. For example, the following insertion causes the phrase "Hello, world" to be displayed to the standard output stream.

cout << Greetings;

The extraction operator is also defined in the iostream library for a right operand that is a char array. The extraction by default first skips leading whitespace characters and then extracts the next sequence of non-whitespace character from the input stream. A null ‘\0′ is automatically stored in the array element that occurs after the element that holds the last extracted character.

It's very calm over here, why not leave a comment?

Leave a Reply

You must be logged in to post a comment.