Strings

 < Day Day Up > 



A string in C++ is an array of characters terminated by the null character ‘\0’. The following code declares a character array and initializes it to a string literal:

char string1[] = “C++ For Artists”;

You could then print this string to the screen by using the name of the array as is demonstrated by the following code:

cout<<string1<<endl;

You could have initialized the array using an initializer list. Examine the following code:

char string1[] = {'C', '+', '+', ' ', 'F', 'o', 'r', ' ', 'A', 'r', 't', 'i', 's', 't', 's', '\0'};

Notice the explicit inclusion of the ‘\0’ character. You will need to allow for one extra space in any array you intend to use for strings for the purpose of including the null character terminator.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

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