开发者

How to declare array string in c++

开发者 https://www.devze.com 2023-01-20 11:44 出处:网络
This is very basic but so: I want a string with 4 characteres:开发者_开发百科 \"abcd\" how must I declare a new string, like that?

This is very basic but so:

I want a string with 4 characteres:开发者_开发百科 "abcd"

how must I declare a new string, like that?

char *newStr = new char[4]; // or -> char newStr[4];

strcpy(newStr, "abcd");

the null '\0' character must be on the size of the string, so new char[5]?


yes, \0 character is a part of string and you must allocate memory for it as well


Yes, the termination nul character is part of the string. So you need to allocate space for 5 characters:

char *newStr = new char[5];
strcpy(newStr, "abcd");

Don't forget to free the dynamically allocate memory once you are done using it as:

delete[] newStr;

Alternatively you can also do:

char newStr[] = "abcd";

In C++ it's better to use the string class for representing strings:

string newStr = "abcd";


You don't have "new" in C, but only in C++.

You could:

char* string = "abc";

or

char string[] = "abc";

or

char* string = (char*) malloc(sizeof(char) * 4);
strcpy(string, "abc");
string[3]='\0';
/* remember to free the used memory */

or

char string[] = { 'a', 'b', 'c', '\0' };


This should do the dirty work for you:

char newString[] = "abcd";

Also, yes, you need new char[5];


If this is C++ (seems re-tagged for what i've read), whats wrong with

std::string my_string = "abcd"; 

?

Isn't that what you are looking for ? I Could be missing something.


/ ------------------- string constructors ----------------

string emp("");              // constructs the "empty string"
string emp2;                 // constructs another "empty string"
string spc(" ");             // constructs string containing " "
string sstr("Some string");  // string containing "Some string"
char frob[] = "Frobnitz";
string sfrob(frob);          // constructs a C++ string containing
                             //   "Frobnitz" from a C-style string
string bar = "foobar";       // string containing "foobar"

// ------------------- stdout, c_str() --------------------

Source: http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/string-class


If it is a string literal, you can do either of these:

char *string = "abcd";
char astring[] = "abcd";

If you want to know this for eventually copying strings, you can either have a buffer, or allocate memory, and space for '\0' is necessary, even though strlen("abcd") will return 4 because it does not count the terminator.

/* buffer way */
char string[5];
strcpy(string, "abcd");

/* allocating memory */
// char *ptr = malloc(5*sizeof(*ptr)); // question was retagged C++
char *ptr = static_cast<char*>(malloc(5*sizeof(*ptr));
strcpy(ptr,"abcd");
/* more stuff */
free(ptr);

Since the question has been retagged to C++, you can also use new and delete.

/* using C++ new and delete */
char *ptr = new char[5]; // allocate a block of 5 * sizeof(char)
strcpy(ptr, "abcd");
// do stuff
delete [] ptr; // delete [] is necessary because new[] was used

Or you could also use std::string.

0

精彩评论

暂无评论...
验证码 换一张
取 消