Solved

String initialization in C++

Posted on 2006-11-28
4
4,068 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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