[英]error: incompatible types when assigning to type ‘BusRoute’ {aka ‘struct <anonymous>’} from type ‘void *’
I created a typedef struct named BusRoute and a pointer to an array of BusRoute named ActiveBusRoutes.我创建了一个名为 BusRoute 的 typedef 结构和一个指向名为 ActiveBusRoutes 的 BusRoute 数组的指针。
typedef struct
{
unsigned int busRouteNumber;
char* startingLocation;
char* endingLocation;
Time routeStartTime;
Time routeEndTime;
}BusRoute;
BusRoute* ActiveBusRoutes[100];
And I need a function to detect 'empty' elements in the ActiveBusRoutes array, ie wherever the pointer is NULL.我需要一个 function 来检测 ActiveBusRoutes 数组中的“空”元素,即指针是 NULL 的地方。
BusRoute* loopBus(){
BusRoute* br; BusRoute* start=*ActiveBusRoutes;
for( br=start; br<start + 100; br++){
if(*br= NULL){
return br;
exit;
}
}
}
When I use gcc to check, it report error, how to fix it plz当我使用gcc检查时,它报告错误,如何修复它
error: incompatible types when assigning to type ‘BusRoute’ {aka ‘struct <anonymous>’} from type ‘void *’
78 | if((*br)= NULL){
=
means assignment and the code is trying to assign NULL where it shouldn't. =
表示分配,代码试图将 NULL 分配到不应分配的位置。
==
means test for equality. ==
表示测试是否相等。
With a change, this will serve you better, but always ends on the first found empty slot in the array.通过更改,这将为您提供更好的服务,但始终在数组中找到的第一个空槽处结束。
BusRoute **loopBus() {
const int nRoutes = sizeof ActiveBusRoutes/sizeof ActiveBusRoutes[0];
for( int i = 0; i < nRoutes; i++ )
if( ActiveBusRoutes[ i ] == NULL )
return &ActiveBusRoutes[ i ]; // ptr to empty spot in array
return NULL; // No empty spots; all are used
}
EDIT: Now returns non-NULL pointer to first empty slot, or returns NULL if there are no empty spots... Notice function declaration.编辑:现在返回指向第一个空槽的非 NULL 指针,或者如果没有空点则返回 NULL ......注意 function 声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.