0

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

3
  • new is not a C keyword. Perhaps you meant to tag this question as c++? Commented Oct 13, 2010 at 18:25
  • your're right, I'll change the title Commented Oct 13, 2010 at 18:35
  • 7
    Use std::string. Don't do it by hand. Commented Oct 13, 2010 at 18:46

7 Answers 7

4

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

Sign up to request clarification or add additional context in comments.

Comments

4

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";

Comments

3

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' };

7 Comments

(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: right (in C++ but not C), wrong (in both), wrong (in both).
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.
@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.
|
1

This should do the dirty work for you:

char newString[] = "abcd";

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

Comments

1

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.

Comments

0

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

1 Comment

No, this is the string class, only available in the stardard library in C++
0

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.

2 Comments

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. :)
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*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.