Solved

String initialization in C++

Posted on 2006-11-28
4
4,058 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 337
maze travler 6 66
max float value 3 59
c++  placing data into a form and an editbox 5 23
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 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