Initialization of C array at time other than declaration?

前端 未结 5 481
[愿得一人]
[愿得一人] 2020-12-17 10:40

I know in C that I can do the following.

int test[5] = {1, 2, 3, 4, 5};

Now this is only legal when declaring the array. However I was wond

相关标签:
5条回答
  • 2020-12-17 10:54

    The reason for that at the surface is that arrays are almost everywhere converted to a pointer to the first element. If you have two arrays

    double A[5];
    double B[5];
    

    in an expression such as

    A = B;
    

    both are already converted to pointers. The A on the left is in particular not an "lvalue" so you can't assign to it.

    This sounds like a lame excuse, but my guess is that historically it just happened like that. C (and C++ with it) was trapped in an early syntax choice and if you want to stay compatible with legacy code there is probably not much way out of it.

    0 讨论(0)
  • 2020-12-17 10:57

    Note that the C99 compound literal allows you to 'pass arrays to functions':

    int your_func(int *test)
    {
        ...use test as an array...
    }
    
    void other_func(void)
    {
        int x = rand();
        if (your_func((int[]){ 0, 1, 2, x, 3, 4 }) > 0 ||
            your_func((int[]){ 9, x, 8, 7, 6, 5 }) > 0)
            ...whatever...
    }
    

    This isn't the same as re-initializing an array with different values, but it may be sufficiently close that it works for you.

    0 讨论(0)
  • 2020-12-17 10:59

    It's not just arrays, you cannot provide an initializer for anything at any point other than in a definition. People sometimes refer to the second statement of something like int i; i = 0; as "initializing i". In fact it's assigning to i, which previously holds an indeterminate value because it wasn't initialized. It's very rarely confusing to call this "initializing", but as far as the language is concerned there's no initializer there.

    Assignment and initialization are separate things to the language, even though they both use the = character. Arrays are not assignable.

    The reason arrays are not assignable is covered elsewhere, for example Why does C++ support memberwise assignment of arrays within structs, but not generally?. The short answer is, "historical reasons". I don't think there's any killer technical reason why the language could not be changed to allow array assignment.

    There's a secondary issue, that grammatically {1, 2, 3, 4, 5} is an initializer, not an array literal, and hence could not be used in assignment even if arrays were assignable. I'm not sure exactly why C89 doesn't have array literals, probably just nobody got around to requiring them. C99 introduces a syntax for "compound literals" in general and array literals in particular: (int[]) {1, 2, 3, 4, 5}. You still can't assign to an array from it.

    0 讨论(0)
  • 2020-12-17 10:59

    The point is that using int array[]={ } is declaring and initializing the object that you have created.

    You can actually assign values to an array after it has been declared:

    int array[5];
    array[0] = 1, array[1] = 2, ...
    

    What you were doing was assigning several values to one single array entry:

    array[5] = {1,2,3,4,5}; // array[5] can only contain one value
    

    This would be legal instead:

    array[5] = 6;
    

    Hope this makes sense. Just a question of syntax.

    0 讨论(0)
  • 2020-12-17 11:00

    Here's The Solution

    //Declaration
    int a[5];
    int a_temp[5] = {1, 2, 3, 4, 5 };
    memcpy(a, a_temp, sizeof(a));
    //Now array a have values 1, 2, 3, 4, 5
    
    0 讨论(0)
提交回复
热议问题