Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. const char* to char* conversion

const char* to char* conversion

Scheduled Pinned Locked Moved Managed C++/CLI
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    auralius manurung
    wrote on last edited by
    #1

    I've got friend suggesting me to convert cons char* variable to char* by copying the data. I mean using: char *x = strdup(const char *y). But I think I can easily typecasting it, like this: x = (char *) y. Am I doing a good coding practice here because I prefer typecasting it I don't even get warning for doing this. Any way are they the same? Which one should be a good programmer do? THX!!!!

    From Indonesia with love..!!

    M K 2 Replies Last reply
    0
    • A auralius manurung

      I've got friend suggesting me to convert cons char* variable to char* by copying the data. I mean using: char *x = strdup(const char *y). But I think I can easily typecasting it, like this: x = (char *) y. Am I doing a good coding practice here because I prefer typecasting it I don't even get warning for doing this. Any way are they the same? Which one should be a good programmer do? THX!!!!

      From Indonesia with love..!!

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      auralius wrote:

      Am I doing a good coding practice here

      No. Casting away const is bad. Without the const it implies the location is writable, which it is not, otherwise it wouldn't be const :) You'd only need a strdup() duplicate of the string if you're going to alter its contents. Static casting is rarely necessary so I always consider it bad practice. Casts are not for fixing compiler errors and warnings!

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • A auralius manurung

        I've got friend suggesting me to convert cons char* variable to char* by copying the data. I mean using: char *x = strdup(const char *y). But I think I can easily typecasting it, like this: x = (char *) y. Am I doing a good coding practice here because I prefer typecasting it I don't even get warning for doing this. Any way are they the same? Which one should be a good programmer do? THX!!!!

        From Indonesia with love..!!

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #3

        Casting is always "dangerous", so think what you do. It is really often the course of strange bugs and problems, which appear months or years after writing the crap. (believe me) Types are your helpers, if it is a const think why!!! Why isnt char* x const? :rolleyes: x = (char *) y : the same string to which are pointing two pointers. In your case with strdup you got another string !!! (you must understand that, or stop coding :~) PS: I compile my Release builds always with warning level 4

        Greetings from Germany

        A 1 Reply Last reply
        0
        • K KarstenK

          Casting is always "dangerous", so think what you do. It is really often the course of strange bugs and problems, which appear months or years after writing the crap. (believe me) Types are your helpers, if it is a const think why!!! Why isnt char* x const? :rolleyes: x = (char *) y : the same string to which are pointing two pointers. In your case with strdup you got another string !!! (you must understand that, or stop coding :~) PS: I compile my Release builds always with warning level 4

          Greetings from Germany

          A Offline
          A Offline
          auralius manurung
          wrote on last edited by
          #4

          yup....i think you booth are right.. i've got some old functions which return a const char* variable... that's confusing me since i need also to modify the result... thanks kastenk thanks mark

          From Indonesia with love..!!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups