The C ++ standard is clear:
A regular string literal is of the type "array of n const char" (section 2.14.5.8 in C ++ 11).
and
The effect of trying to change an undefined string literal (section 2.14.5.12 in C ++ 11).
For a string known at compile time, a safe way to get non-const char* is this
char literal[] = "teststring";
you can safely
char* ptr = literal;
If at compile time you don't know the string, but you know its length, you can use an array:
char str[STR_LENGTH + 1];
If you do not know the length, you will need to use dynamic allocation. Make sure you free up memory when strings are no longer needed.
This will only work if the API does not get ownership of the char* you are passing.
If he tries to free the lines inside, then he should say this in the documentation and tell you about the correct way to select lines. You will need to map the selection method to the one used inside the API.
char literal[] = "test";
will create a local array of 5 characters with automatinc memory (this means that the variable will be destroyed when execution leaves the area in which the variable is declared) and initialize each character in the array with the characters' t ',' e ',' s', ' t 'and' \ 0 '.
You can subsequently edit these characters: literal[2] = 'x';
If you write this:
char* str1 = "test"; char* str2 = "test";
then, depending on the compiler, str1 and str2 can be of the same value (i.e. point to the same line).
("Regardless of whether all string literals are different (that is, stored in objects that do not support overlapping), it is determined by the implementation." In section 2.14.5.12 of the C ++ standard)
It may also be true that they are stored in a read-only memory section, and therefore any attempt to change the line will result in an exception / failure.
They are also, in fact, of type const char* , so this line:
char * str = "test";
actually discards the constant in the string, so the compiler will give a warning.