2013-03-11 163 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接口和實現指出,「這個定義是合法的,因爲結構,聯合和枚舉標記佔用與變量,函數和類型名稱空間分開的同一名稱空間。「

相關問題