【发布时间】:2010-04-21 18:02:28
【问题描述】:
typedef struct
{
uint32 item1;
uint32 item2;
uint32 item3;
uint32 item4;
<some_other_typedef struct> *table;
} Inner_t;
typedef struct
{
Inner_t tableA;
Inner_t tableB;
} Outer_t;
Outer_t outer_instance =
{
{NULL},
{
0,
1,
2,
3,
table_defined_somewhere_else,
}
};
我的问题是如何检查 tableA 是否为 NULL,就像 outer_instance 的情况一样。
它尝试过:
if ( tmp->tableA == NULL )。我得到“错误:二进制的无效操作数==”
/****************************编辑:更新的代码如下 ************ *****************/
我已根据 bta 的建议更改了代码,但现在又遇到了另一个错误 “错误:初始化元素不是常量”在 NULL 下的行(在 items_instance)
typedef struct
{
uint32 item1;
uint32 item2;
uint32 item3;
uint32 item4;
} Items_t;
typedef struct
{
Items_t *itemsA;
Items_t *itemsB;
} Outer_t;
Items_t items_instance=
{
1,
2,
3,
4
};
Outer_t outer_instance=
{
NULL,
items_instance
};
【问题讨论】:
-
毫无意义。
tableA不能是NULL。字段tableA具有Inner_t类型。Inner_t是一个结构,而不是一个指针。将结构对象与NULL进行比较毫无意义,这是编译器告诉你的。解释你想要做什么。 -
我理解你的观点 AndreyT。我知道编译器告诉我我不能将结构对象与 NULL 进行比较,因为它不是指针。看,我需要检查 Outer_t 结构中的 tableA 是否为空/NULL 或尚未定义或不存在(无论正确的术语是什么),然后使用 tableB。仅此而已。还有第二种选择。即制作“Inner_t *tableA;”在 Outer_t. 但是我怎么能用 tableA empty/NULL 实例化 outer_instance ...
-
如果 tableA 是
Inner_t *tableA,你可以将它初始化为 NULL,就像你现在的代码一样。