What... the... heck? Const Char & Chars.
Ok, I am SO sick of this error. C++ just won't let me compare char's to actual text. In recent code I've had to actually define certain letters I wanted to check for in arrays.
What's wrong with that? Nothing as far as I can tell! But I get 3 errors:
C never did this, why is C++?
Code:
char hello[] = {"h", "w", "w"};Quote:/Users/gareth/desktop/sizeOf Test/main.cpp:5: error: invalid conversion from 'const char*' to 'char'
/Users/gareth/desktop/sizeOf Test/main.cpp:5: error: invalid conversion from 'const char*' to 'char'
/Users/gareth/desktop/sizeOf Test/main.cpp:5: error: invalid conversion from 'const char*' to 'char'
C never did this, why is C++?
Jones Wrote:What's wrong with that? Nothing as far as I can tell! But I get 3 errors:Code:
char hello[] = {"h", "w", "w"};
"h" and "w" happen to be const char*s. so {"h", "w", "w"} is actually a const char*[]. You probably want to either do
Code:
char hello[][] = {"h", "w", "w"};or
Code:
char hello[] = "hww";
or
char[] = {'h', 'w', 'w'};
Notice the single quotes.
char[] = {'h', 'w', 'w'};
Notice the single quotes.
"When you dream, there are no rules..."
Note that if you use the last method, it won't be NULL terminated, so anything you do with the string using anything that assumes it's a standard C string will likely crash.
akb825 Wrote:Note that if you use the last method, it won't be NULL terminated, so anything you do with the string using anything that assumes it's a standard C string will likely crash.wouldnt adding that fix that then? like this:
Code:
char[] = {'h', 'w', 'w', '\0'};oh and, if you do this:
Code:
char[] = {"h", "w", "w"};As usual with programming, it's the little things that make the difference
Yes, that would.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| char array versus pointer to string constant question | WhatMeWorry | 7 | 6,642 |
Jan 30, 2007 12:26 PM Last Post: bronxbomber92 |
|
| Killing the char on the end of a char array | wyrmmage | 3 | 2,943 |
Dec 4, 2006 08:55 PM Last Post: wyrmmage |
|
| two-dimensional dynamic array, const char* adding | wyrmmage | 2 | 4,358 |
Nov 22, 2006 04:53 PM Last Post: wyrmmage |
|
| Carbon Key / Char Codes | dave05 | 0 | 3,238 |
Sep 25, 2005 08:41 AM Last Post: dave05 |
|
| handle to char* | LongJumper | 1 | 2,481 |
May 18, 2005 11:09 PM Last Post: LongJumper |
|

