我有以下代码:(我只保留了我的问题的相关部分)
// Structure
struct first_list {
char firstname[100];
struct first_list * next;
};
struct last_list {
char lastname[100];
struct last_list * next;
};
void verify_name()
{
char firstname[100];
char lastname[100];
struct first_list * curr_first = loaded_first;
struct last_list * curr_last = loaded_last;
/////////// this part was added by me
while( curr_last != NULL )
{
strncpy(lastname, curr_last->lastname, sizeof( lastname ) - 1 );
valid = validate_name(sock,buffer,sizeof(buffer),firstname);
if(valid < 1)
fprintf(stderr, "%s is VALID\n", lastname);
else
fprintf(stderr, "%s is invalid\n", lastname);
curr_last = curr_last->next; //this is used for removing current record
}
printf( "LastName validation complete\n" );
break;
/////////// end of part added by me
while( curr_last != NULL && curr_first != NULL )
{
strncpy(firstname, curr_first->firstname, sizeof( firstname ) - 1 );
strncpy(lastname, curr_last->lastname, sizeof( lastname ) - 1 );
////////////////////////////////
//// irrelevant code (functions)
////////////////////////////////
if( curr_first->next == NULL && curr_last->next != NULL)
{
curr_last = curr_last->next;
curr_first = loaded_first;
}
else
curr_first = curr_first->next;
}
}
因此,我在同时验证名字和姓氏的函数之前添加了一个新函数来验证姓氏(以优化整个过程)。问题是,如果我删除 break 命令,我会收到一个错误,因为验证后 last_list 为空。
我怎样才能只保留结构中的有效姓氏,因为现在当移动到下一条记录时,它正在删除当前记录,直到循环完成(NULL)?