Cannot assign an array to another

后端 未结 4 638
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 02:52

I tried through different ways to copy an array pointer to another one, without any success. Here are my attempts, with the associated error message.

typedef         


        
相关标签:
4条回答
  • 2020-12-07 03:10

    You cannot assign arrays in C. Use memcpy to copy an array into another.

    coordinates coord2;
    
    memcpy(coord2, coord, sizeof coord2);
    
    0 讨论(0)
  • 2020-12-07 03:22

    Your coordinates need to be initialized as pointers

    coordinates *coords2, *coords3;

    Try that out and then assign.

    0 讨论(0)
  • 2020-12-07 03:32

    You cannot assign one array to another using the assignment operator, e.g. a = b, because an array doesn't know the size of itself.

    This is the reason why we cannot assign one array to another using the assignment operator.

    0 讨论(0)
  • 2020-12-07 03:33

    When arrays are passed by value, they decay to pointers. The common trick to work around that is wrapping your fixed-size array in a struct, like this:

    struct X {
        int val[5];
    };
    
    struct X a = {{1,2,3,4,5}};
    struct X b;
    b = a;
    for(i=0;i!=5;i++)
        printf("%d\n",b.val[i]);
    

    Now you can pass your wrapped arrays by value to functions, assign them, and so on.

    0 讨论(0)
提交回复
热议问题