View Full Version : how to convert const char* to char*
dineshb_2001
03-09-2004, 03:09 PM
please can you tell me how to convert const char* to char*.
dinesh.
please can you tell me how to convert const char* to char*.
dinesh,
If I am not suffering some kind of old-age lysdexia, I remember this is not possible: there is no way to convert from const char* to char*. A const char* is a pointer to a CONSTANT memory area. The idea here is that trying to write to such location will be validated by the compiler and will always fail. (That is why it is constant)
The only workaround I know of is to copy the data to a char* variable and use it.
char const* is a different scenario, it means a CONSTANT pointer to a char location, in this case you can modify the information contained in the memory location, however, you cannot modify the pointer itself.
Examples:
const char *x;
char const *a = x;
char *b;
char *c = "ABCDEF";
b = (char*)malloc(sizeof(char)*120);
memcpy(b, "Anything", 9);
// Illegal
memcpy(c, b, 5); // c points to a "constant" memory area
// Legal
x = b; // x (the pointer) can be modified
// Illegal
memcpy(x, "1234", 5); // Although the memory is not constant, x is a pointer to constant memory, so this should not be allowed
// Illegal
a = x; // a cannot point to any other place (a cannot be modified)
// Legal
free(b);
b = strdup(c);
// Now you can use a COPY of the data that is constant in a non-constant memory area
// and so on...
I'm a bit rusty on this issue anyway, so I suggest you test these cases.
Hope this helps
RobSeace
03-09-2004, 09:42 PM
Yeah, Loco is technically correct, however if all you're trying to
do is satisfy function prototype checking or something similar,
then all you really should need to do is a simple cast... Eg: if
you've got a function that returns "const char *" (I hate functions
declared that way, for this very reason, BTW), and you're trying
to pass the retval to some standard function declared to take a
simple "char *", then you can just do "(char*) func(...)"... Of course,
you better make damn sure that whatever you're passing it to
doesn't really modify the string, either! (And, in the modern ANSI
world, you technically should prototype such functions that don't
modify their string args to take "const char *"s, so there's no issue,
anyway... However, I know many people don't do so for their
own functions... I sure as hell don't... ;-) And, we have a huge
library of functions that don't, as well... So, I've run into this sort
of thing on a few occassions...)
Basically, I guess it depends on what exactly you're trying to do
by converting from one to the other... More info about what you're
doing will let us give you better advice...
Wow, for the first time in my life I am technically correct...
OK, you have the easiest, shortest and nicest answer, but mine is technically correct!!! I like it...
Anyway, I think what you suggest is what most sane people will do...
mishravik
05-24-2004, 02:48 PM
Hi dinesh,
I thing you can const_cast for this converting const char* to char*.
Of course you can modify content of const char* pointer.
Regards,
Vikas Mishra
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.