C 错误:取消引用指向不完整类型的指针
浏览了许多其他与此相关的SO帖子,但没有一篇能够帮助我。因此,我定义了以下结构:C 错误:取消引用指向不完整类型的指针,c,C,浏览了许多其他与此相关的SO帖子,但没有一篇能够帮助我。因此,我定义了以下结构: typedef struct { int created; double data; int timeLeft; int destination; }dataPacket; typedef struct { dataPacket *array; int currIndex; int firstIndex; int nextTick;
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--;
}
}
}
int sendPacket(节点*n,int勾选)
{
如果(n->buffer->length>0)
{
如果(n->backoff lconnection->busy!=0 | n->lconnection->busy!=0)
{
n->i++;
n->backoff=(512/W*genrand()*(pow(2,n->i)-1))/勾选长度;
}
其他的
{
n->L连接->忙=1;
n->R连接->忙=1;
n->lconnection->currPacket=n->buffer[n->buffer->currendex];
n->rconnection->currPacket=n->buffer[n->buffer->currenindex];
}
}
其他的
{
n->退避--;
}
}
}
每次尝试访问缓冲区、lconnection或rconnection的成员时,我都会遇到标题中描述的错误
struct packetBuffer *buffer;
您已经定义了一个类型packetBuffer
(一个用于匿名结构的typedef)
您尚未定义struct packetBuffer
如果没有现有类型struct packetBuffer
,编译器会将其视为不完整类型,并假定您稍后会完成它。宣言
struct packetBuffer *buffer;
是完全合法的,但是除非类型struct packetBuffer
可见,否则不能取消对buffer
的引用
只需删除struct
关键字
(我个人的偏好是放弃typedef
,并始终将结构类型称为struct which
,但这是风格和品味的问题。)以下内容:
typedef结构{
int x;
char*y;
...
}我的结构代码>
为匿名结构创建标识符。为了使结构引用自身的实例,它不能是“匿名的”:
这意味着my_struct\t
现在是类型struct my_struct
,而不仅仅是匿名结构。另外,请注意,struct my_struct
可以在其自身的结构定义中使用。这在匿名结构中是不可能的
最后一个复杂问题是,struct my_struct
中的my_struct
与my_struct
位于不同的“名称空间”。这有时用于简化(或混淆)代码中的内容,如:
typedef struct my_struct {
int x;
char *y;
struct my_struct *link
....
} my_struct;
现在我可以在代码中的任何地方使用my_-struct
,而不是struct my_-struct
最后,您可以将typedef与结构定义分开,以实现相同的效果:
struct my_struct {
int x;
char *y;
struct my_struct *link;
....
};
typedef struct my_struct my_struct;
正如David R.Hanson在《C接口和实现》中指出的那样,“这个定义是合法的,因为结构、联合和枚举标记占用同一个名称空间,该名称空间与变量、函数和类型名称空间是分开的。”如果(n->lconnection->busy!=0 | n->lconnection->busy!=0)
。。。为什么有一个条件相同的或
语句?@d0rmLife:大概RHS应该参考r连接
@KeithThompson…毫无疑问…!;)
struct my_struct {
int x;
char *y;
struct my_struct *link;
....
};
typedef struct my_struct my_struct;