how to initialize arrays properly?

Jan 21, 2012 at 5:58pm
i have recently been struggling with arrays on my bloodshed dev-cpp compiler - according to Bjarne Stroustrups book, i should be able initialize arrays like this: char letters[26] = {"a", "b", "c", "d", "e", "f" "g"...}. However, this is a supposed invalid conversion from const *char to char. I have realized that this works for me -
 
    char letters[26] = {"abcdefghijklmnopqrstuvwxyz"};
as does this
char letters[52] = {"a b c d e f g h i j k l m n o p q r s t u v w x y z"};
I was presuming that Stroustrups methods would work - his way of initializing arrays is common across the web, and in other books. Also, the escape thingymagiggy \0 doesn't seem to make a difference
I was just wondering whether i'm wrong, or if c++ has evolved again. If i am wrong, what are the pros and cons of doing it my way(why can it be done several ways?)?.
Thanks
Mike
Last edited on Jan 21, 2012 at 6:02pm
Jan 21, 2012 at 6:02pm
Double quotes are for strings. Use single quotes for chars. char letters[26] = {'a', 'b', 'c' ...}
Last edited on Jan 21, 2012 at 6:03pm
Jan 21, 2012 at 6:04pm
Oh, ok. thankyou, i feel silly
Topic archived. No new replies allowed.