casting a const char into a char
What happens when you cast a const char* to a char*?
Code:
const char* str1 = "test"; |
Hi
Short answer: no :) But I'll try to explain. Strings in C are confusing. It's usually better to think of them as an array of characters. These lines are equivalent: Code:
const char *str = "test"; Code:
char *str2 = (char*) str1; To make a new string that's a pointer to heap memory that you can modify and then free, use the strdup function. Code:
char *str2 = strdup(str1); |
Right. I saw this in some code, and I can't figure out why the author wanted to cast out the const'ness of the string
|
Quote:
|
All times are GMT -5. The time now is 10:02 AM. |