RetroX
|
 |
Reply #15 Posted on: March 05, 2010, 09:37:59 PM |
|
|
Master of all things Linux
Contributor
Posts: 746
|
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'}; char y[]={'d','a','m','n','i','t','\0'}; char *z=x+y;or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit");
|
|
|
|
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
|
|
|
Josh @ Dreamland
|
 |
Reply #16 Posted on: March 05, 2010, 09:54:21 PM |
|
|
Pragma
Dev Team
Posts: 1078
|
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is. "Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them. Also, you don't need to use const_cast to get it represented as a const char.  Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
|
|
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
RetroX
|
 |
Reply #17 Posted on: March 05, 2010, 10:04:53 PM |
|
|
Master of all things Linux
Contributor
Posts: 746
|
The only difference between const char* and char* is that const char* points to read-only memory. And even that's only on systems that support such (ie, not Windows). '\0' == 0, and I'd be willing to bet that if you were to compare {'n','o',0} and "no" you'd get that they were equivalent. And I do mean as-is.
"Test" == "test" compares correctly for string literals. "Test" == "test" will return false, "test" == "test" will return true. This is because they point to the same location in memory when GCC is done with them.
Also, you don't need to use const_cast to get it represented as a const char.  Const char* can be set to a char* without cast. Vice-versa requires cast, but is dangerous on Linux and the like.
Whenever I have tried to do it without const_cast, G++ bugs me about a depreciated conversion (that works). I always use -Wall and purge all warnings and errors, and that is one that I happen to get into the habit of doing.
|
|
|
|
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
|
|
|
score_under
|
 |
Reply #18 Posted on: March 06, 2010, 08:59:16 AM |
|
|
Member
Posts: 314
|
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'}; char y[]={'d','a','m','n','i','t','\0'}; char *z=x+y;or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit"); Wait, what?
|
|
|
|
|
Logged
|
|
|
|
RetroX
|
 |
Reply #19 Posted on: March 06, 2010, 03:25:17 PM |
|
|
Master of all things Linux
Contributor
Posts: 746
|
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'}; char y[]={'d','a','m','n','i','t','\0'}; char *z=x+y;or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit"); Wait, what?
Exactly. It's pointless.
|
|
|
|
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
|
|
|
score_under
|
 |
Reply #20 Posted on: March 06, 2010, 08:30:41 PM |
|
|
Member
Posts: 314
|
That's const char*
char* would mean something like this:
char x[]={'h','e','l','l','o',',',' ','\0'}; char y[]={'d','a','m','n','i','t','\0'}; char *z=x+y;or
char *z=const_cast<char*>("hello, ")+const_cast<char*>("damnit"); Wait, what?
Exactly. It's pointless.
Tell me... char x[]={'h','e','l','l','o',',',' ','\0'}; char y[]={'d','a','m','n','i','t','\0'}; char *z=x+y; printf("%c",z[2]);...What happens when you run that? If you say "prints l" I'll shoot your face off. If you say "access violation because you added 2 random pointers", then I'll forgive you... for now.
|
|
|
|
|
Logged
|
|
|
|
miky
|
 |
Reply #21 Posted on: March 06, 2010, 09:45:48 PM |
|
|
Resident Troll
Posts: 130
|
Of course it's adding two random pointers. He was explaining what I meant when I asked if you could overload operator+ for two char *s.
|
|
|
|
|
Logged
|
|
|
|
score_under
|
 |
Reply #22 Posted on: March 06, 2010, 10:15:45 PM |
|
|
Member
Posts: 314
|
I see, though overloading the + for a char* is really pointless IMO.
|
|
|
|
|
Logged
|
|
|
|
Josh @ Dreamland
|
 |
Reply #23 Posted on: March 06, 2010, 10:18:28 PM |
|
|
Pragma
Dev Team
Posts: 1078
|
Yeah, compiler presently concatenates "" + "" automatically. For "" + anything else, I want it to cast first "" to variant.
|
|
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
|
|