Results 1 to 3 of 3

Thread: const char* to char*

  1. #1

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228

    const char* to char*

    How do you convert a variable of type 'const char*' to 'char*'?
    To protect time is to protect everything...

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    You don't really - here's why

    Accesible memory is either read-only or read/write. You cannot change memory the compiler designated as read-only to be read/write.

    const means read-only

    try moving the data do a different place in memory that is read/write like:
    Code:
    const char *s="hi there";
    char tmp[20];
    strcpy(tmp,s);
    // tmp is read/write

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Generic way:
    const char *str = "something";
    char * copy = new char[strlen(str)];
    strcpy(copy, str);
    // don't forget to delete[] copy when you're done

    Oh, strdup() does just that, but it uses malloc, so you must free() the copy.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width