const char* to char* conversion
-
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..!!
-
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..!!
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:
-
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..!!
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
-
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
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..!!