Solved

String initialization in C++

Posted on 2006-11-28
4
4,040 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
ID: 18033302
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
ID: 18033949
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
ID: 18035145
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
ID: 18038174
>>>> 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

932 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

12 Experts available now in Live!

Get 1:1 Help Now