What... the... heck? Const Char & Chars.

Jones
Unregistered
 
Post: #1
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.
Code:
char hello[] = {"h", "w", "w"};
What's wrong with that? Nothing as far as I can tell! But I get 3 errors:

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++?
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #2
Jones Wrote:
Code:
char hello[] = {"h", "w", "w"};
What's wrong with that? Nothing as far as I can tell! But I get 3 errors:

"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";
Quote this message in a reply
Moderator
Posts: 522
Joined: 2002.04
Post: #3
You want single quotes instead of double quotes for a literal char.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #4
or

char[] = {'h', 'w', 'w'};

Notice the single quotes.

"When you dream, there are no rules..."
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #5
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.
Quote this message in a reply
capzkilla
Unregistered
 
Post: #6
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"};
Since the compiler "thinks" they are all strings, every character will be automaticly NULL terminated, so you'd be trying to put 3 strings in a char array, while you wanted to put 3 ascii characters in a char array.
As usual with programming, it's the little things that make the difference Wink
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #7
Yes, that would.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  char array versus pointer to string constant question WhatMeWorry 7 7,552 Jan 30, 2007 12:26 PM
Last Post: bronxbomber92
  Killing the char on the end of a char array wyrmmage 3 3,461 Dec 4, 2006 08:55 PM
Last Post: wyrmmage
  two-dimensional dynamic array, const char* adding wyrmmage 2 4,997 Nov 22, 2006 04:53 PM
Last Post: wyrmmage
  Carbon Key / Char Codes dave05 0 3,654 Sep 25, 2005 08:41 AM
Last Post: dave05
  handle to char* LongJumper 1 3,094 May 18, 2005 11:09 PM
Last Post: LongJumper