how to initialize all elements of an array at one go when definition and declaration are separate?

徘徊边缘 提交于 2021-02-05 07:49:30

问题


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 ?


回答1:


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:

int arr[5];

/* ... */

memcpy(&arr, &(int [5]){ 0 }, sizeof arr);

With GCC's typeof extension, you can add a little more safety:

memcpy(&arr, &(typeof(arr)){ 0 }, sizeof arr);

In C89 you must give the source array a name:

{ static const int zero[5] = { 0 }; memcpy(&arr, &zero, sizeof arr); }



回答2:


During definition, you can do this assignment.

However, arr[5] means trying to assign value to the 5th index and it expects a single integer value, not values inside curly braces to indicate array initialization.




回答3:


int arr[5] means an array of integers with place to hold 5 values having index 0,1,2,3,4. now arr[5] would not point to any element in this array.

u could use

arr[] = {0,0,0,0,0} knowing that there are 5 elements in your array.

or may be memset() could help you.




回答4:


The syntax of C is such that arr alone decays to &a[0], the address of the first element, in almost all contexts. So the syntax that would be natural for assigning to arrays arr = { ... }; can't work. So no assignment to an array as a whole is not possible.

The syntax int a[5] = { 0 }; is initialization and this works that it initializes all elements with 0. The best you can do is to always initialize arrays with this "catch all" initializer to have all elements in a known state. Then, if later in your program you decide that you want different values, assign them directly.




回答5:


First, please note the linguistic difference between initialization and assignment. The former occurs when a variable is given a value at the same time as it is declared, while the latter is everything else giving the variable a value in runtime. If you set all elements of a variable in a loop, you are doing so in runtime, and therefore assigning values to the array.

The {} is called an initializer list. Initializer lists can only occur at the same line as the variable declaration, and all items inside the list must be constants (assuming C90 standard). They can be used to initialize both arrays or structs (a.k.a "aggregates"), same rules apply to both.

int arr[5] = {0};

What this does in detail, is to set the first element at index [0] to zero. The remaining relements are initialized according to a rule which states that if only a few elements are given in an initializer list, the elements that weren't explicitly initialized by the programmer shall be set to zero by the compiler (ISO 9899:1999 6.7.8 §19).

So the line above sets arr[0] to zero, and then the compiler sets the remaining elements, arr[1] to arr[4], to zero as well. Had you written

int arr[5] = {1};

then the elements of the array would have been initialized to {1, 0, 0, 0, 0}.



来源:https://stackoverflow.com/questions/4859915/how-to-initialize-all-elements-of-an-array-at-one-go-when-definition-and-declara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!