【发布时间】:2017-10-09 15:36:34
【问题描述】:
谁能告诉我解释一下,如何将结构传递给函数?我试图将我的排序放入一个函数中,并将我的结构传递给它
typedef struct
{
int weight;
int price;
Color color;
Equip equip;
}Cars;
Cars automobil[5];
sort_cars(&automobil[NUMBER_OF_CARS]);
void sort_cars(struct Cars*automobil[NUMBER_OF_CARS]){
int i,j;
CarsmobilOne={};
for(j=0; j<NUMBER_OF_CARS-1; j++)
{
for (i=0; i<NUMBER_OF_CARS-1; i++){
if (automobil[i]->weight < automobil[i+1]->weight)
{
continue;
}else{
mobilOne = automobil[i];
automobil[i] = automobil[i+1];
automobil[i+1] = mobilOne;
}
}
}
我收到此错误“从类型'struct Cars *'分配给类型'Cars'时不兼容的类型|”我试图像人们在互联网上那样传递结构
【问题讨论】:
-
唉,在哪一行?请发帖minimal reproducible example。
-
在这一行:mobilOne = automobil[i];
-
您没有在代码中的任何位置定义
struct Cars -
将
struct Cars更改为Cars。您从未将Cars定义为结构标记,仅定义为typedef。 -
一个错误是关于你如何声明结构的。您使用 typedef 创建一个
struct Cars,但您给函数一个struct CarsnotCars这是您创建的结构。
标签: c function struct parameter-passing