1
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)是不允许的。我现在该怎么办?

4

2 回答 2

1

假设联系人有一个成员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 */
}
于 2013-02-28T19:38:54.213 回答
1

只是为了好玩:-)(其他解决方案很好)

#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 =="" */;
}
于 2013-03-01T08:49:52.990 回答