when we write something like this
int arr[5] = 0;
or int arr[5] = {0};
there is no problem
but when we do something like this
int arr[5];
arr[5] = {0};
an error occurs. Any explanation for this ?
arraysc++initialization
when we write something like this
int arr[5] = 0;
or int arr[5] = {0};
there is no problem
but when we do something like this
int arr[5];
arr[5] = {0};
an error occurs. Any explanation for this ?
Best Answer
It is simply a part of the language definition that arrays can be initialised, but not directly assigned. You can do what you want in C99 using
memcpy()
with a compound literal:With GCC's
typeof
extension, you can add a little more safety:In C89 you must give the source array a name: