Solved

String initialization in C++

Posted on 2006-11-28
4
4,027 Views
Last Modified: 2009-08-21
Hi Experts,

In C, if you want to initialize a char string you would set it to null. But, in C++ string how do I do it?  I think C++ string doesn't terminate on null character.....

char * myStr = "\0";
0
Comment
Question by:ambuli
4 Comments
 
LVL 3

Expert Comment

by:Akumas
Comment Utility
just declare a std::string object, and it's much safer than c-style char*, little chance to corruption;

initialize:
#include <string>
using std::string;

sttring myStr;//initialized

myStr = "some contant";//assign value

myStr.clear;//clear content in vc7 and above

myStr = "";//clear content in vc6

const char* cStyle = myStr.c_str();//get c style str
0
 
LVL 15

Expert Comment

by:efn
Comment Utility
When you construct an object of the std::string class from the standard library, it is empty by default, so if that's what you want, you need not do any more to initialize it.  If you want to get rid of the content of a string, you can use the clear function as Akumas showed, except you need parentheses:

myStr.clear();
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
btw, instead of this :

    char * myStr = "\0";

this is sufficient :

    char * myStr = "";

The '\0' character is automatically appended by using "" for initialising.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
Comment Utility
>>>> In C, if you want to initialize a char string you would set it to null.

Actually, that statement is wrong.

   char* s = NULL;

sets the char *pointer* to NULL what cannot be called an initialization of a string.

In C/C++ you can initialize a char array by

    char sz[100] = { '\0' };

That sets all 100 chars to zero.

Look at that:

   char* psz = "Hello";

that is different to

   char sz[] = "Hello";

In the first case you have a pointer variable that points to an address in memory where the string "Hello" terminated by a zero character is stored. The storage is constant and if you overwrite it, it may crash (depending on the compiler you are using).

In the second case you have a writable char buffer of size 6 which can be overwritten:

   sz[0] = 'h';  

In both cases you can't enlarge the string e. g. by
 
   strcat(sz, " Joe);    // it most likely crashes or makes some other variable corrupt cause you write beyond array bounds.

All that you can avoid by using a string class as Akumas and efn have suggested.

   std::string s = "Hello";
   s += ' ' + "Joe";

Regards, Alex


0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now