gpt4 book ai didi

不能将一个数组分配给另一个

转载 作者:行者123 更新时间:2023-12-03 16:18:09 27 4
gpt4 key购买 nike

我尝试通过不同的方式将一个数组指针复制到另一个数组指针,但没有成功。
这是我的尝试,以及相关的错误消息。

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 coordinate* coordinates;相反,但对我来说它看起来不是很明确。

最佳答案

你不能在 C 中分配数组。使用 memcpy将一个数组复制到另一个数组中。

coordinates coord2;

memcpy(coord2, coord, sizeof coord2);

关于不能将一个数组分配给另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10862550/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com