tags:

views:

147

answers:

7

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]?

+4  A: 

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

Vladimir
A: 

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.

birryree
It should be pointed out that your first two suggestions seem to imply that the language is C - yet I don't think C has static_cast or templates. :) If you do, you're using C++, and therefore, you can avoid using malloc for good measure. :)
Arafangion
The original question was tagged 'C', so I originally wrote C code with `malloc`. It was later retagged C++ so I changed the original `malloc` example to cast the return `void*`.
birryree
+3  A: 

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' };
vulkanino
(char*) cast is not needed, sizeof(char) is always 1, string[3]='\0' is not needed, strcpy makes this
the cast is needed, sizeof(char) may not be 1, strcpy does not copy the terminatig char. edit: the cast is not needed by the compiler, I advice to keep it for clarity.
vulkanino
@vulkanino: right (in C++ but not C), wrong (in both), wrong (in both).
Steve Jessop
In C, the cast from `malloc` is unnecessary, `sizeof(char)` is always 1 as defined in the C Standard, and `strcpy` does copy the `\0` byte.
birryree
See 5.3.3/1 in the C++ standard. 6.5.3.4/3 and 7.21.2.3/2 in C99. I don't have the C89 reference for `strcpy`, which is what C++ refers to in 21.4/2.
Steve Jessop
@vulkanino: the cast is *not* necessary in C as of C89 and its use is discouraged. It is necessary in C++, but you shouldn't be using `malloc()` in C++ code. And `strcpy()` will write the terminating `0` to the target string.
John Bode
@vulkanino I see you edited your comment. In C++ the cast is necessary, but if this were C code, doing the cast can lead to problems a la http://c-faq.com/malloc/mallocnocast.html
birryree
+1  A: 

This should do the dirty work for you:

char newString[] = "abcd";

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

jetru
A: 

/ ------------------- 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

halfevil
No, this is the string class, only available in the stardard library in C++
vulkanino
Crap, My Apologies.
halfevil
+4  A: 

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";
codaddict
+1  A: 

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.

Tom