不能将数组分配给另一个 [英] Cannot assign an array to another

查看:449
本文介绍了不能将数组分配给另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过不同的方式来一个数组的指针复制到另一个,没有任何成功尝试。
这里是我的尝试,与相关的错误消息。

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 long int coordinate;
typedef coordinate coordinates[3];

void test(coordinates coord) {
    coordinates coord2 = coord; // error: invalid initializer
    coordinates coord3;
    coord3 = coord; // error: incompatible types when assigning to type ‘coordinates’ from type ‘long int *’
    coord3 = (coordinates) coord; // error: cast specifies array type
    coord3 = (coordinate[]) coord; // error: cast specifies array type
    coord3 = (long int*) coord; // error: incompatible types when assigning to type ‘coordinates’ from type ‘long int *’
}

我知道我可以使用的typedef协调*坐标; 代替,但它看起来并不很明确的给我

I know I could use typedef coordinate* coordinates; instead, but it does not look very explicit to me.

推荐答案

您不能在C.使用指定数组的memcpy 到一个数组复制到另一个。

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

coordinates coord2;

memcpy(coord2, coord, sizeof coord2);

这篇关于不能将数组分配给另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆