not all char arrays are strings. Only char arrays that are null terminated are strings. Therefore:
Code:
char cArray[15];
for(i = 0; i < 15; ++i)
{
cArray[i] = 'a';
}
is valid but cArray is not a string meaning you won't be able to do this:
cout << cArray << endl;
In order to display elements of cArray (as utilized above) to the screen you will need to use a loop like this:
Code:
for(i = 0; i < 15; ++i)
{
cout << cArray[i];
}
Doing so you will indeed have a row of 15 a's on the screen.
To have cArray hold 15 a's as a string you will need to have cArray contain 16 char, 15 for the a's and 1 for the null terminating char.
Code:
char cArray[16];
for(i = 0; i < 15; ++i)
{
cArray[i] = 'a';
}
cArray[++i] = '\0';
cout << cArray << endl;
//or
char cArray[16];
cout << "enter 15 a's in a row, then press enter" << endl;
cin >> cArray;
cout << cArray;
the >> operator tacks on a null terminating char to the array for you, as will other istream methods like get() or getline().
It is this ease of use, that is, no need to use loops to input and display elements of a char array, that makes strings so useful.
Also note that when using the new operator:
char * p;
p = new char; //p is a pointer to space for a single char
delete p;
p = new char[16];//p (for all practical purposes) is now an empty char array, not a string, and won't be a string unless the char array is null terminated.
delete [] p;