0
const int array[]  = {1,2};
const int array[2] = {1,2};

Both compile and work with no problems. Is there any difference in these ?

(I use Codevision, but that shouldn't really matter)

NikkyD
  • 2,209
  • 1
  • 16
  • 31

1 Answers1

5

No, there is no difference.

The only exception is for the 2nd case if one initialises a char array with a string literal and the array's size does not reflect the '\0'-terminator, then the latter gets chopped off.

char s[] = "alk" // makes s 4 chars wide
char s[3] = "alk" // makes s 3 chars wide

For all other types or kinds of initialisation the compiler should warn about a too large initialiser.

If the initialiser is "smaller" then the array, the remaining elements are initialised as if they were defined at globale scope, that is as if they were static.

All this is completely unrelated to whether anything in this context is const or not.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 2
    You might add that if you have less values than elements to initialize, the remaining are filled with 0. – Gerhardh May 25 '18 at 12:47