![]() |
#2
pkwangxinjun2011-01-11 13:13
|

void traversal(Object *obj)
{
for (int i = 0 ;i<20; i++)
{
if (obj[i].nId==NULL)
{
cout<<"物品编号:"<<obj[i].nId+1<<endl;
cout<<"物品名称:"<<obj[i].szName<<endl;
cout<<"物品位置:"<<obj[i].szAddr<<endl;
cout<<endl;
}
}
}struct Object
{
int nId; //编号
char szName[20]; //名字
char szAddr[50]; //存放位置
};
{
for (int i = 0 ;i<20; i++)
{
if (obj[i].nId==NULL)
{
cout<<"物品编号:"<<obj[i].nId+1<<endl;
cout<<"物品名称:"<<obj[i].szName<<endl;
cout<<"物品位置:"<<obj[i].szAddr<<endl;
cout<<endl;
}
}
}struct Object
{
int nId; //编号
char szName[20]; //名字
char szAddr[50]; //存放位置
};
这个是一个遍历的函数。由指针传入。
下面的代码就是那个结构体的定义。
问题就是:如何判断结构体为空。
我尝试这用结构体里面的某一个变量,比如:nId;
使用:if (obj[i].nId!=NULL)
判断这个时候是否为空。如果不为空的话,那么就输入数据。
可是奇怪的是:我使用obj[i].nId==NULL(即是为空),能正确输入。
而使用:if (obj[i].nId!=NULL)(即是不为空),却输入了烫烫烫烫烫烫烫烫之类的字符。