0

My question is fairly simple, are using curly braces in array initializer lists necessary? In school, we were taught to declare and initialize arrays as such:

char FullAlphabet[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"};

but I'm not noticing any difference, when debugging, when declaring and initializing the array without the curly braces:

char FullAlphabet[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";

Is there a difference between these two initializations? If not, any reason to use one over the other? Thank you.

JosephTLyons
  • 2,075
  • 16
  • 39
  • 1
    Side note: you may as well get rid of the `52`. – barak manos Jul 29 '16 at 10:45
  • @barakmanos Oh yes, since the compiler will automatically detect the size after initializing. I seem to forget this at times. Thanks. – JosephTLyons Jul 29 '16 at 10:45
  • 1
    I'd use curly braces only if it was an array of `char*`, not sure what the language standard says about your first example. Perhaps it permits it, but it sure as hell doesn't make sense to me. It makes sense to use brackets when initializing it with a list of single characters (i.e., `{'A',' ','B',' ',...,' ','Z'}`). – barak manos Jul 29 '16 at 10:47
  • 1
    There's no difference since double quotes define a string literal which are treated as `const char[]` arrays by the compiler. Be careful though should you assign string literals to non-const `char*` pointers. As of C++11, you'd have to cast it before assignment. – Jonathon Ogden Jul 29 '16 at 10:49

0 Answers0