Why and when do we have to initialize a string before using in C++? -


I was reading some C ++ tutorials and found that sometimes a string is initialized ('initial' May not be a proper word) Never

Why and when?

The code I was reading was:

  four names [50]; Four last names [50]; Four full names [100]; ... cin.getline (name, 50); ... cin.getline (last name, 50); Full name [0] = '\ 0'; // search for '\ 0' for cat after strcat (full name, name); // Copy the name to full name (full name, ""); // We want to separate names by a space strcat (full name, last name); // Last Name   

Finally please copy the last name Please someone please explain

This is similar to any other datatype. By default, local variables have the value of garbage. So, you initialize it if you are reading its value. But if you know that you will be writing in the variable, then you can skip the initialization part.

For example:

  char a [20]; // local variable   

is garbage, so you have to write some meaningful data before reading it. But if you are writing this, you can leave the initiality.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -