I was reading some C++ tutorial and find that sometimes a string is initialized ('initialized' may not be the proper term), sometimes not.
Why and when?
The code I was reading is:
char name[50];
char lastname[50];
char fullname[100];
...
cin.getline ( name, 50 );
...
cin.getline ( lastname, 50 );
fullname[0] = '\0'; // strcat searches for '\0' to cat after
strcat ( fullname, name ); // Copy name into full name
strcat ( fullname, " " ); // We want to separate the names by a space
strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
Someone please kindly explain.
name,lastnameare not given initial values because they are given values withcin.getline().fullnamecould be initialised in this situation but for whatever reason it is not. In cases such as this however, I think it is preferable to usestd::stringandstd::getlinerather than plain char arrays.std::string, it initializes the variable so there are no worries.