RetroX
|
|
Reply #15 Posted on: March 05, 2010, 09:37:59 pm |
|
|
Master of all things Linux
Location: US Joined: Apr 2008
Posts: 1055
|
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)Why do all the pro-Microsoft people have troll avatars?
|
|
|
Josh @ Dreamland
|
|
Reply #16 Posted on: March 05, 2010, 09:54:21 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
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
Location: US Joined: Apr 2008
Posts: 1055
|
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)Why do all the pro-Microsoft people have troll avatars?
|
|
|
score_under
|
|
Reply #18 Posted on: March 06, 2010, 08:59:16 am |
|
|
Joined: Aug 2008
Posts: 308
|
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
Location: US Joined: Apr 2008
Posts: 1055
|
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)Why do all the pro-Microsoft people have troll avatars?
|
|
|
score_under
|
|
Reply #20 Posted on: March 06, 2010, 08:30:41 pm |
|
|
Joined: Aug 2008
Posts: 308
|
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
|
|
|
|
|
|
Josh @ Dreamland
|
|
Reply #23 Posted on: March 06, 2010, 10:18:28 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
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
|
|
|
|
|