How to assign a string constants to an array of characters?

The online answer probably is this:
char c[50];
string s = "hello ni mei";
strcpy(c,s);
However, if the length of the string does not know, or the length of the string is very small, you define a large array of characters, not to waste any memory?
Or is this?
int len = s.length();
char c[len];//C++ inside this statement cannot pass.?

Started by Augus at February 02, 2016 - 2:12 PM

The new version of the C language can be, this is called a variable length array

Posted by Simon at February 12, 2016 - 2:22 PM

His new[] delete[]

Posted by Andy at February 23, 2016 - 2:42 PM

Dynamic allocation! The length, in the allocation of space!

Posted by Cinderella at November 15, 2016 - 8:39 AM

The definition of char* C, then the dynamic management by using malloc/free.

Posted by Adonis at November 28, 2016 - 9:06 AM

The use of dynamic array is good
char* c = new char[len];

Posted by Hobart at December 11, 2016 - 9:58 AM

I use
char c[50];
string s = "hello ni mei";
strncpy(c,s.c_str(),49);c[49]=0;

Posted by Kenny at December 21, 2016 - 10:42 AM

Waste this memory is not afraid, as long as there is no stack overflow, even.

Posted by Lillian at January 01, 2017 - 11:18 AM

Yes [] is put inside the constant char str[] = "1234";

Posted by Christian at January 02, 2017 - 12:59 PM

The use of pointer
char *s = "1234567";
int len = strlen(s);
Establish
char *p = new char[len];
Delete
delete [] p;
p = NULL;

Posted by Spike at January 06, 2017 - 1:21 PM

Create the wrong
Need to pay attention to at the end of the string 需要注意 字符串尾部的 \0

Posted by Andy at January 09, 2017 - 11:22 AM

This time is generally used to create dynamic memory space, such as the char *pBuff = new char[len+1]; // set aside an end

Posted by Burgess at January 10, 2017 - 12:17 PM