Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String initialization 1

Status
Not open for further replies.

ssmitty

Programmer
Sep 5, 2002
9
0
0
US
How do you initialize a string? Is there a way to do it as simply as you initialize a variable?
 
I think you need to specify what kind (type) of 'string' you want to initialize.

It could be CString, std::string, char[], ... and probably a few(?) more.


/JOlesen
 
Oh, sorry, i want to initialize a char[] string.
 
I just copied this from the documentation :

Character arrays can be initialized in one of two ways:

Individually, as follows:
char chABCD[4] = { 'a', 'b', 'c', 'd' };
With a string, as follows:
char chABCD[5] = "abcd";
In the second case, where the character array is initialized with a string, the compiler appends a trailing '\0' (end-of-string character). Therefore, the array must be at least one larger than the number of characters in the string.

Because most string handling uses the standard library functions or relies on the presence of the trailing end-of-string character, it is common to see unbounded array declarations initialized with strings:

char chABCD[] = "ABCD";


Thanks a lot Microsoft.

/JOlesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top