UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » how to convert const char* to char*

#1 2004-03-09 04:09 PM

dineshb_2001
Member
From: germany
Registered: 2004-01-30
Posts: 10

Re: how to convert const char* to char*

please can you tell me how to convert const char* to char*.


dinesh.


take the world in ur stride.

Offline

#2 2004-03-09 07:35 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: how to convert const char* to char*

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...

Offline

#3 2004-03-09 10:42 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: how to convert const char* to char*

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...

Offline

#4 2004-03-10 01:40 AM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: how to convert const char* to char*

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...

Offline

#5 2004-05-24 02:48 PM

mishravik
Guest

Re: how to convert const char* to char*

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

  • Index
  • » C
  • » how to convert const char* to char*

Board footer

Powered by FluxBB