2013-03-11 167 views
2

通过其他许多与此相关的SO帖子,但没有人能够帮助我。所以,我有以下结构定义:错误:取消引用指向不完整类型的指针

typedef struct 
    { 
    int created; 
    double data; 
    int timeLeft; 
    int destination; 
}dataPacket; 

typedef struct 
{ 
    dataPacket *array; 
    int currIndex; 
    int firstIndex; 
    int nextTick; 
    int maxLength; 
    int length; 
    int stime; 
    int total; 


}packetBuffer; 

typedef struct{ 
    int mac; 
    struct wire *lconnection; 
    struct wire *rconnection; 
    int numRecieved; 
    struct packetBuffer *buffer; 
    int i; 
    int backoff; 
}node; 

typedef struct{ 
    float length; 
    float speed; 
    int busy; 
    struct dataPacket *currPacket; 
    struct node *lnode; 
    struct node *rnode; 
}wire; 

然后我尝试使用以下功能:

int sendPacket(node *n, int tick) 
{ 
    if(n->buffer->length > 0) 
    { 
     if(n->backoff <= 0) 
     { 
      if (n->lconnection->busy != 0 || n->lconnection->busy != 0) 
      { 
       n->i++; 
       n->backoff = (512/W * genrand()*(pow(2,n->i)-1))/TICK_LENGTH; 
      } 
      else 
      { 
       n->lconnection->busy = 1; 
       n->rconnection->busy = 1; 
       n->lconnection->currPacket = n->buffer[n->buffer->currIndex]; 
       n->rconnection->currPacket = n->buffer[n->buffer->currIndex]; 
      } 
     } 
     else 
     { 
      n->backoff--; 
     } 
    } 
} 

我得到的称号,每次描述的错误我尝试访问缓冲区,lconnection或rconnection的成员。

+0

'if(n-> lconnection-> busy!= 0 || n-> lconnection-> busy!= 0)'...为什么有一个具有相同条件的'或'语句? – d0rmLife 2013-03-11 18:13:19

+0

@ d0rmLife:推测RHS应该参考'rconnection' – 2013-03-11 18:15:22

+0

@ KeithThompson ...毫无疑问......!) – d0rmLife 2013-03-11 18:16:20

回答

5
struct packetBuffer *buffer; 

您已经定义了一个类型packetBuffer(其他匿名结构的typedef)。您还没有定义struct packetBuffer

如果缺少现有类型struct packetBuffer,则编译器将其视为不完整类型,假定您稍后将完成它。声明

struct packetBuffer *buffer; 

是完全合法的,但你不能解引用buffer除非类型struct packetBuffer是可见的。

只需删除struct关键字。

(我个人的偏好是删除typedef,始终是指结构类型为struct whatever,但是这是风格和口味的问题。)

1

以下:

typedef struct { int x; char *y; ... } my_struct;

创建匿名结构的标识符。为了,一个结构,指的是自身的实例,它不能是“匿名”:

typedef struct my_struct { 
    int x; 
    char *y; 
    struct my_struct *link 
    .... 
} my_struct_t; 

这意味着my_struct_t现在是类型struct my_struct,而不仅仅是匿名结构。此外,请注意struct my_struct可以在其自己的结构定义中使用。匿名结构是不可能的。

作为最后的复杂情况,struct my_struct中的my_structmy_struct_t处于不同的“命名空间”。这有时被用来简化(或混淆)的代码这样的事情:

typedef struct my_struct { 
    int x; 
    char *y; 
    struct my_struct *link 
    .... 
} my_struct; 

现在我可以在我的代码,而不是struct my_struct使用my_struct任何地方。

最后,你可以在类型定义从结构定义分离,以达到同样的效果:

struct my_struct { 
    int x; 
    char *y; 
    struct my_struct *link; 
    .... 
}; 
typedef struct my_struct my_struct; 

正如大卫R.Hanson的I2C接口和实现指出,“这个定义是合法的,因为结构,联合和枚举标记占用与变量,函数和类型名称空间分开的同一名称空间。“

相关问题