struct contact list[3];
int checknullarray()
{
for(int x=0;x<10;x++)
{
if(strlen(contact[x].name)==0)
{
return x;
break;
}
}
}
我遇到了 checknullarray 的问题。它说我的类型名称(contact[x].name)
是不允许的。我现在该怎么办?
假设联系人有一个成员char name[n];
struct contact list[3];
int checknullarray(void) /* void is a better option when no params */
{
for (int x = 0; x < 10; x++) /* 3 or 10 ? I think you want x < 3 */
{
/*
if(strlen(contact[x].name)==0) No need to strlen, you can check if name[0] == 0
*/
if (list[x].name[0] == '\0')
{
return x;
/*
break; why break if you return in previous line?
*/
}
}
return x; /* As suggested by qPCR4vir you need an alternative return */
}
只是为了好玩:-)(其他解决方案很好)
#include "contact.h"
#define N 3
struct contact list[N];
int checknullarray(void)
{
int x;
for ( x = 0; x < N && *list[x].name; x++) ;
return x;
}
int main(void)
{
/* initialice list */
int R;
R=checknullarray();
if (R==N) /* No name in list =="" */;
}