tags:

views:

237

answers:

2
int main()

{
   char *second= new char("hello");
   char *first="hi";
   char third[]="new";
}

I am new to c++ and don't really understand how char works, why the first one give a compiler error, and what are the differences of those 3 way of declaration , the strength and benefits of declaring it in a particular way.

Thanks

Hmm, as someone mentions that the second form is read only, why could I Change it. suppose I have the code below

 int main()

{
   char *second= new char("hello");
   char *first="hi";
   char third[]="new";
   first="world";
}

code like above will still execute, why is it so? , then which form is better if I want to read an input but don't know the size of the string?

+10  A: 

Know that

"abc"

allocates static storage somewhere, which lasts the whole program lifetime. You cannot write to that storage, so C++ gives it the type char const[N] (an array of N constant characters). Now, the following makes a pointer point to that storage

char *first = "hi";

Since that drops a const, that way of initializing the pointer is deprecated. That it works at all is just to keep backward compatibility with C, where a string literal does not have a const type (but is still read only). Prefer the following instead

char const *first = "hi";

In constrast, the last way you have shown copies the string literal's content to an array, which will be writable, and be sized so the string literal just fits into it.

char third[] = "new";

If you do that in a function, then as all variables, that array will be cleaned up when you leave its scope. Now, the first way you have shown is different. It creates a character dynamically. You could have initialized it like this

char *c = new char('A');

And since that happens dynamically, you need to tell the compiler explicitly when it should free the memory

delete c;

But you cannot initialize the character with a string literal. What you probably had in mind is creating storage dynamically, initialized with the string literal. That's not possible using new. The only form of initializing a dynamic array is to zero it out, but you cannot directly-initialize with the content of a string literal or another array. For this form of using new, there is rarely a need to do that directly. If you want, you can do it by creating a dynamic array of the right size, and then copy bytes from the string literal to that buffer

char *c = new char[sizeof "hello"]; // sizeof "hello" will give 6
std::strcpy(c, "hello");

delete[] c; // delete[] is for deleting dynamic arrays

Remember that this is quite low-level, and i recommend you to use strings

std::string s = "hello"; // s.size() gives you its size

It completely manages memory for you. Concatenation, indexing and that stuff is available too.

Johannes Schaub - litb
+1. The only thing I would add is a small bit about char vs. char*. Reading the OP's question, it seems like he's confused about char vs. char*/c-style strings.
kbyrd
Thanks. added some other stuff
Johannes Schaub - litb
good summary man.Don't you think there's a problem with newcomers relying on std::string too much. It makes C++ too much like Java and when you have to work with char* you're lost.
toto
+1  A: 

Lets try to explain in code:

// your code
char *first="hi";
// this is the memory location of the string, assigned by the compiler
// sizeof(first) == sizeof(char*) == 4 (usually, lets not get into this right now)
char *first = 0x12345;


// your code
char third[]="new";
// means, the following:
char third[4];
third[0] = 0x6E; // ascii code of 'n'
third[1] = 0x65; // ascii code of 'e'
third[2] = 0x77; // ascii code of 'w'
third[3] = 0x00; // strings in C end with NULL
// note that sizeof(third) is still sizeof(char*), but this 
// time you statically allocated sizeof(char)*4 for the whole array

More reading for you:

elcuco