[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

String initialization in C++

Posted on 2006-11-28
4
Medium Priority
?
4,091 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

650 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