Here's my code...
char* m1;
char* m2 = "sth";
strcpy(m1, m2);
That code threw run-time errors, so I tried...
char m1[];
char m2[] = "sth";
That can run with no errors. However, I want to use...
char* s
What sould I do?
Here's my code...
char* m1;
char* m2 = "sth";
strcpy(m1, m2);
That code threw run-time errors, so I tried...
char m1[];
char m2[] = "sth";
That can run with no errors. However, I want to use...
char* s
What sould I do?
You should allocate
memory for m1
.
For example
char* m1 = new char[4];
char* m2 = "sth";
strcpy(m1, m2);
// so smth with m1
delete[] m1;
But since you write on C++, why you not use std::string
?
Problem is you are not allocating any memory to store the result of the strcpy method. You need:
char* m1 = (char *) malloc(sizeof(char) * 4) /* 4 being the size of 3 chars + trailing \0 */
char* m2 = "sth";
Than you can do the strcpy
And in the end you have to free any dynamically allocated memory a.g.
free(m1)
m2 is allocated statically and doesn't has to be freed.
In C++ you would rather do:
// don't forget to #include <string>
std::string m1;
std::string m2 = "sth";
m1 = m2;
if you then need a const char*
(e.g., for some API call) you can get one
const char* str = m1.c_str();
Problem gone.
Plus, you don't need to bother with buffer sizes and proper deallocation anymore.
I believe even your 2nd example is also invalid
char m1[];
char m2[] = "sth";
m1 is not having any valid piece of memory to store the copied data. And, I believe you should have the array dimension in your declaration (you are not declaring a method param here)
Normally you should have something like
char m1[10];
char m2[] = "sth";
strcpy(m1, m2);
Go back to the question. Many people said you should allocate memory in order to use char*. It is not wrong, but more accurately, you should have the pointer pointing to valid piece of memory.
Allocating memory is one way, but you can also do something like this:
char outdata[100];
char* m1 = &(outdata[10]);
char* m2 = "foo";
strcpy(m1, m2);
no explicit allocation here, and still a valid example.
I believe OP is still not understanding what is an array and what is a pointer. Get some extra reading and thinking on that.
First of all, before you perform the copy you must assure that the destination pointer have enough memory to keep the copy, if you don't you'll get runtime errors. So you need this:
char* m1;
char* m2 = "sth";
m1 = new char[strlen(m2)+1]; // <---- reserve memory for m1
strcpy(m1, m2);
// remember to delete[] m1
Another way to deal with character strings is to use the std::string
class instead, if you do, the memory management will not bother you.
std::string m1("");
std::string m2("sth");
m1 = m2; // no strcopy needed, no memory management needed
Finally, if for some reason you must deal with pointers, take care of the new/delete pair and remenber that the memory reserved with new[] must be deleted wit delete[].
And and additional advice: instead of memcpy
i preffer the use of std::copy
:
char* m1;
char* m2 = "sth";
m1 = new char[strlen(m2)+1]; // <---- reserve memory for m1
std::copy(std::begin(m2), std::end(m2), std::begin(m1));
// remember to delete[] m1
Because it works both for the char *
and the std::string
version if you use std::begin
and std::end
functions:
std::string m1("");
std::string m2("sth");
std::copy(std::begin(m2), std::end(m2), std::begin(m1));
Edit: It works as long the m2
are statically reserved so the length can be deduced at compile time.