C++ copy const char* to char*
Solution 1
Like so:
argv[1] = new char[length +1](); // () to value-initialize the array
Your version:
argv[1] = new char(length +1);
only allocates a single char
and value-initializes it to length+1
.
Solution 2
You have two problems in your code:
-
You need to add 1 to length after copying in order to copy null character (as strlen returns only number of chars without null character; see more here). So change code to:
strncpy(argv[1], filePath, length + 1);
-
You need fix how your array is being initialized as you are initializing only one character (and we assume you want full string to be copied). So:
argv[1] = new char[length + 1]();
Notes:
- Please when you post also post the code that was used to print out data as problems such as these in a lot of cases depend on what you call to print out data.
- And at the end you might consider using just an array of fixed size that is initialized to maximum path. For max path size in windows checkout following post
Solution 3
The problem is that you're using strncpy
, rather than strcpy
. And
the way you're using it, it doesn't copy the terminating \0
.
In practice, because strncpy
may leave a string without a \0
terminator, it's best to avoid it. In your case, strcpy
alone is fine, since you've just allocated a sufficiently large buffer. In the more general case, you may have to use strlen
, to ensure that the string you have fits in the target buffer (without ever forgetting to add 1 to the results, for the \0
).
If the situation occurs a lot, you might want to write your own version
of strncpy
, which works (i.e. guarantees a terminating \0
, and
doesn't copy or write more characters than necessary). Something like:
void
stringCopy( char* dest, int maxLength, char const* source )
{
assert( maxLength > 0 );
char* end = dest + maxLength - 1;
while ( dest != end && *source != '\0' ) {
*dest = *source;
++ dest;
++ source;
}
*dest = '\0';
}
(This function actually exists, under the name strcpy_s
in C 2011, but
it isn't widely implemented; Microsoft has it, but I've not seen it
elsewhere.)
leon22
Updated on June 29, 2022Comments
-
leon22 almost 2 years
I have a function
ClassA::FuncA(const char *filePath)
and want to copy this const char string* to a char*!
My solution:
char *argv[2]; int length = strlen(filePath); argv[1] = new char(length +1); strncpy(argv[1], filePath, length);
after this I have in argv[1] the desired chars but also some other undefined chars!
filePath:
"C:\Users\userA\Parameter.xmlþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþKŸQyá•"
Whats wrong here? The length with strlen is OK!
-
smerlin about 12 years
argv[1] = new char[length+1]; strncpy(argv[1], filePath, length+1);
could be faster for large lengths?! Well in most cases it wont matter i guess. -
smerlin about 12 yearsi pressed enter for a newline ...well does not work in comments, so my incomplete comment was visible for a second. you are to fast!
-
James Kanze about 12 yearsHis use of
strncpy
is also wrong (but the syntax error in thenew
was well spotted).