First thing, in C++ (or C) a character in single quotes is just a single lone character, but any characters in double quotes is a string literal. for example "hello"
is a const char[6]
containing the characters 'w'
,'o'
,'r'
,'l'
,'d'
,'\0'
. That extra '\0'
is the null terminator that makes an array of chars into a c-string.
Your code is making a variable
char* myStrings[] = { "a", "c", ... };
This is an array of char*
, which means each element is a char*
, which in turn means each element is a full string (in your case each has 2 characters).
You want an array of single characters, which means you either drop the *
in char*
, or drop the []
in myStrings[]
, and use either an array of single characters {'a','b',...}
, or a single string literal "acdfbevBC"
. Any of these lines will work in your code:
const char * myStrings = "acdfbevBC";
char myStrings[] = "acdfbevBC";
char myStrings[] = { 'a', 'c', 'd', ... 'C', '\0'};
If you never plan to print myStrings
as a unified string, you can leave off the '\0'
, the null terminator is there so functions that you pass it to know when it stops without you having to explicitly pass how long the string is.
Also, the first line is const char *
because you should never overwrite a string literal. char myStrings[]
is fine without const because the compiler is using the literal to initialize an array that gets its own place in memory, not making myStrings
point into a separate cache of string literals.
Check out cpp reference's details here for more details on string literals.
myStrings
to be an array of strings, or an array of c-strings (char*) , or an array of individual characters? the name suggents one thing, butdisplayChar(myStrings[i]);
seems to suggest the other. – BrettAM Mar 26 '15 at 23:01char
, a c-style string of one or more characters is achar*
. I'm still not certian, do you wantmyStrings[i]
to be the i'th single character from the string "acdfbevBC", or do you want it to be the i'th of 9 unrelated strings? – BrettAM Mar 27 '15 at 16:16