In my project there is a method which only returns a const char*
, whereas I need a char*
string, as the API doesn't accept const char*
.
Any idea how to convert between const char*
to char*
?
In my project there is a method which only returns a const char*
, whereas I need a char*
string, as the API doesn't accept const char*
.
Any idea how to convert between const char*
to char*
?
To make sure you don't break stuff, make a copy of the returned string.
The function returning const char*
expects this string will never be changed. Therefore things can/will break if your code or the API you pass it make a change after all.
Even worse, if a change is made, your program is likely to crash you in case the returned string was literal (e.g. "hello I'm a literal string"
) because they are (often) stored in memory that can't be written to.
You could use strdup()
for this, but read the small print. Or you can of course create your own version if it's not there on your platform.
First of all you should do such things only if it is really necessary - e.g. to use some old-style API with char*
arguments which are not modified. If an API function modifies the string which was const originally, then this is unspecified behaviour, very likely crash.
Use cast:
(char*)const_char_ptr
To make sure you don't break stuff, make a copy of the returned string.
The function returning const char*
expects this string will never be changed. Therefore things can/will break if your code or the API you pass it make a change after all.
Even worse, if a change is made, your program is likely to crash you in case the returned string was literal (e.g. "hello I'm a literal string"
) because they are (often) stored in memory that can't be written to.
You could use strdup()
for this, but read the small print. Or you can of course create your own version if it's not there on your platform.
You can use the strdup
function which has the following prototype
char *strdup(const char *s1);
Example of use:
#include <string.h>
char * my_str = strdup("My string literal!");
char * my_other_str = strdup(some_const_str);
or strcpy/strncpy to your buffer
or rewrite your functions to use const char *
as parameter instead of char *
where possible so you can preserve the const
strdup()
is a matter of 15 minutes. –
Intact if (constptr) {char * p = calloc(strlen(constptr)+1, sizeof(*constptr)); if (!p) fail(); strcpy(p, constptr); my_nonconstptr_func(p); free(p);}
–
Intact A const
to a pointer indicates a "read-only" memory location. Whereas the ones without const
are a read-write memory areas. So, you "cannot" convert a const
(read-only location) to a normal(read-write) location.
The alternate is to copy the data to a different read-write location and pass this pointer to the required function. You may use strdup()
to perform this action.
To convert a const char*
to char*
you could create a function like this :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* unconstchar(const char* s) {
if(!s)
return NULL;
int i;
char* res = NULL;
res = (char*) malloc(strlen(s)+1);
if(!res){
fprintf(stderr, "Memory Allocation Failed! Exiting...\n");
exit(EXIT_FAILURE);
} else{
for (i = 0; s[i] != '\0'; i++) {
res[i] = s[i];
}
res[i] = '\0';
return res;
}
}
int main() {
const char* s = "this is bikash";
char* p = unconstchar(s);
printf("%s",p);
free(p);
}
res
is used. This code res[i] = s[i]
is undefined behavior, most likely crash. –
Goliard You can cast it by doing (char *)Identifier_Of_Const_char
But as there is probabbly a reason that the api doesn't accept const cahr *
,
you should do this only, if you are sure, the function doesn't try to assign any value in range of your const char*
which you casted to a non const one.
For example, you could write this way:
const char* a = "art.bin";
char* b = new char[sizeof(a)];
strcpy(b, a);
new
operator. Please don't use C++ to answer C questions. Additionally, answers are more useful when they explain things rather than just provide a snippet of code. –
Astraea © 2022 - 2024 — McMap. All rights reserved.
strtok()
does) make a copy of the data returned and pass on the copy.strdup()
andfree()
are your friends. – Intactfree()
whatstrdup()
allocated! – Intactconst_cast<>()
– Mobileint execvp(const char *file, char *const argv[]);
and considering I'm using modern C++, my reason for abstracting it is "fuck your arcane bullshit reasons". Except it doesn't work as well as I expected it to, thanks to the fact that argv[], which nobody ever ever ever modifies, is not const. – Downthrow