Does a const array definition with initialization in C require a length parameter?

117 views Asked by At
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)

1

There are 1 answers

1
alk On BEST ANSWER

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.