2
#include <cstring> char* str0; const char* str1 = "abc"; // assign str1 to str0 strcpy(str0, str1); // syntax correct, but run time error str0 = str1; // syntax error, cannot convert const char* to char* string n_str = str1; str0 = n_str; // syntax error, cannot convert ... cout << str0 << endl; // expected output: abc 

I'd like to make str0 same as str1 while runtime(after compilation), I don't know how to do it. And for the case str0 = str1; I don't understand why it won't work, because str0 points to nothing, while str1 points to a const string literal, so if I now make str0 point to what str1 is pointing to, it should be fine, but it is not. So it there any way to solve it?

6
  • 6
    Where should strcpy copy the string? To nowhere? You should first allocate some memory for str0 to point to. Commented Dec 10, 2019 at 8:35
  • Either strdup or allocate memory first. Assignment won’t work because it’s const and you can’t just remove that with assignment. Commented Dec 10, 2019 at 8:36
  • 2
    Literals strings (like "abc") are really arrays of constant characters. Why would you want a non-constant char pointer to such an array? What is the real problem you need to solve? Right now this is too much of an XY problem. Commented Dec 10, 2019 at 8:36
  • @Someprogrammerdude the original problem is, there is a class with one of the member data of type char*, and a constructor. The constructor has one of the parameters of type const char*, the constructor should set the member data as what is passed in the constructor parameter. It's part of homework and I'm not allowed to post it online sorry Commented Dec 10, 2019 at 8:49
  • 1
    You don't have to post your actual code, only a simple minimal reproducible example to illustrate the problem, and of course please ask about the actual problem directly instead. In this case you need to dynamically allocate memory for the string in the object, and copy the contents (and don't forget the string null terminator). Commented Dec 10, 2019 at 8:56

4 Answers 4

5
std::string str0; const std::string str1 = "abc"; // assign str1 to str0 str0 = str1; cout << str0 << endl; // output: abc 

If you insist on using C:

char* str0; const char* str1 = "abc"; str0 = malloc(strlen(str1) + 1); // if you use a c++ compiler you need instead: // str0 = (char*) malloc(strlen(str1) + 1); strcpy(str0, str1); // and free after use // if you use C++ this will not help much // as pretty much any exception above will cause your code to get out of `free`, // causing a memory leak free(str0); 

If you insist on using bad C++:

char* str0; const char* str1 = "abc"; str0 = new char[strlen(str1) + 1]; strcpy(str0, str1); // and delete after use // this will not help much // as pretty much any exception above will cause your code to get out of `delete`, // causing a memory leak delete(str0); 

Please read about RAII to understand why all of the solutions with manual memory management are bad: cppreference , wiki

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

3 Comments

Please note that in C way you should call free afterwards manually to avoid memory leakage
if I do this, and it works, is it the same as your solution? char* str0 = new char; const char* str1 = "abc"; strcpy(str0, str1);
@keanehui1 no. You allocate mem for just 1 char
2

Let's look one issue at a time

strcpy(str0, str1); 

strcpy copies the characters pointed by str1 into the memory pointed by str0. str1 points to "abc", but str0 doesn't point to anything, hence the runtime error. I would recommend using std::string everywhere so you don't have to manage the memory yourself.

str0 = str1; 

str0 is of type char*, str1 is of type const char*. The const qualifier instructs the compiler to not allow data modification on that particular variable (way over simplified role of const, for more in-depth explanation use your favorite search engine and you should be able to find a bunch of articles explaining const). If you'd be able to assign the same pointer to str0 you'd break the const contract; str0 can be modifiable.

string n_str = str1; 

This is valid because std::string overloads the assignment operator and accepts a const char pointer as the right hand value.

str0 = n_str; 

n_str is of type std::string and str0 is char*, there's no overloaded operator that allows this. If you really want the raw point out of an std::string you can use the c_str() method and it will return you a const char* - I strongly advise against it, unless you have to pass it to a function that only accepts const char*.

Comments

1

Copying strings is an expensive operation. But moving strings from one place to another is efficient.

casts away the const

str0 = (char*) str1; 

or use std::string class template library for managing strings. std::string owns the character buffer that stores the string value. characters are part of the string object. cont char* stores the address of such a character buffer but does not own it.

c_str returns a const char* that points to a null-terminated string. It is useful when you want to pass the contents.

std::string str1 = "abc"; char* str0; strcpy(str0, str1.c_str()); printf(str0); 

1 Comment

str1 is a const char *. The std::string was n_str.
0

const is part of the type, and as such, you can cast it "away". This is considered bad practice, but you should think of const as a strong suggestion of the original programmer, not to modify it.

const char * str1 = "abc"; char * str2 = (char*) str1; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.