简体   繁体   English

错误:分配给类型 'BusRoute' {aka 'struct 时的类型不兼容<anonymous> '} 来自类型 'void *'</anonymous>

[英]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.

相关问题 错误:从“void *”类型分配类型“值”时出现不兼容的类型? - Error: incompatible types when assigning to type 'values' from type 'void *'? 编译器错误:在malloc期间从类型&#39;void *&#39;分配给&#39;struct&#39;时出现不兼容的类型 - Compiler error: incompatible types when assigning to 'struct' from type 'void *' during malloc 如何修复错误:返回类型'struct {aka struct时的类型不兼容<anonymous> }</anonymous> - How to fix error: incompatible types when returning type 'struct {aka struct <anonymous>} 从 struct 分配给类型 struct * 时的类型不兼容 - incompatible types when assigning to type struct * from struct 从&#39;ZipperTree&#39;类型分配给&#39;struct ZipperNode&#39;类型时不兼容的类型 - incompatible types when assigning to type ‘struct ZipperNode’ from type ‘ZipperTree’ 错误:从结构卡类型*中分配给“结构卡”类型时类型不兼容 - error: incompatible types when assigning to type ‘struct card’ from type struct card *’ 从“ int”类型分配给“ struct protein”类型时不兼容的类型 - incompatible types when assigning to type 'struct protein' from type 'int' 从Int类型分配给&#39;struct PartyInfo&#39;类型时不兼容的类型 - Incompatible types when assigning to type 'struct PartyInfo' from type Int test.c:51:4:错误:从类型&#39;void *&#39;分配给类型&#39;blk时,类型不兼容 - test.c:51:4: error: incompatible types when assigning to type ‘blk from type ‘void *’ 分配给类型struct时不兼容的类型 - incompatible types when assigning to type struct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM