说我有一个结构,
struct room{
char name[21];
int num1;
int num2;
struct room *doors[4];
};
所以提供了多少房间,
struct room rm[Number_of_room];
和每个房间:
scanf(name | num1 | num2)
strcpy(rm[i].name, name)
rm[i].num1 = num1
rm[i].num2 = num2
一切正常。
直到,我希望检查门是否已经指向某个东西,
UP=0, RIGHT=1, DOWN=2, LEFT=3
rm[i].doors[0] = &rm[j] // this is how I point the door to an other room, works fine too.
但是当我检查strlen或null时,假设我只将一个门从room1分配给room2,room1不知何故有多个非空门。
有没有办法检查门是否空了?
由于
答案 0 :(得分:2)
初始化struct room
类型的新对象时,将其doors
成员设置为NULL。然后比较NULL并查找它们是否已设置
struct room object;
object.doors[0] = NULL;
object.doors[1] = NULL;
object.doors[2] = NULL;
object.doors[3] = NULL;
if (object.doors[2] != NULL) {
/* already assigned something */
/* maybe reset? */
object.doors[2] = NULL;
};