定义delete(student st[],int*itemcount)函数以从student对象数组中删除目标记录

定义delete(student st[],int*itemcount)函数以从student对象数组中删除目标记录,c,arrays,struct,typedef,C,Arrays,Struct,Typedef,我正在努力学习C编程,所以我试着做一些练习。如下所示,搜索函数仅返回1或-1,变量索引用于确定目标学生是否存在。但之后,它又被用来确定它是数组中的最后一个,中间还是第一个。当我们已经将搜索函数返回的值分配给索引时,索引怎么会有那个数值呢?请帮我弄清楚。多谢各位 //function to delete record void delete_rec (student st[], int *itemcount) { char id[10]; int index, i; if

我正在努力学习C编程,所以我试着做一些练习。如下所示,搜索函数仅返回1或-1,变量索引用于确定目标学生是否存在。但之后,它又被用来确定它是数组中的最后一个,中间还是第一个。当我们已经将搜索函数返回的值分配给索引时,索引怎么会有那个数值呢?请帮我弄清楚。多谢各位

//function to delete record
void
delete_rec (student st[], int *itemcount) {
    char id[10];
    int index, i;
    if (*itemcount > 0) {
        printf ("Enter student's ID:");
        scanf ("%s", id);
        index = search (st, id, *itemcount);

        if ((index != -1) && (*itemcount != 0)) {
            if (index == (*itemcount - 1))  //delete the last record
            {

                clean (st, index);
                --(*itemcount);

                printf ("The record was deleted.\n");
            } else      //delete the first or middle record
            {
                for (i = index; i < *itemcount - 1; i++) {
                    st[i] = st[i + 1];
                    clean (st, *itemcount);
                    --(*itemcount);
                }

            }

        } else
            printf ("The record doesn't exist.Check the ID and try again.\n");


    } else
        printf ("No record to delete\n");
}

//function to clean deleted record
void
clean (student st[], int index) {

    strcpy (st[index].stnumber, "");
    strcpy (st[index].stname, "");
    st[index].sex = NULL;
    st[index].quizz1 = 0;
    st[index].quizz2 = 0;
    st[index].assigment = 0;
    st[index].midterm = 0;
    st[index].final = 0;
    st[index].total = 0;

}
//删除记录的函数
无效的
删除记录(学生st[],整数*项目计数){
字符id[10];
int索引,i;
如果(*itemcount>0){
printf(“输入学生ID:”);
scanf(“%s”,id);
索引=搜索(st、id、*itemcount);
如果((索引!=-1)&&(*itemcount!=0)){
if(index==(*itemcount-1))//删除最后一条记录
{
清洁(st,指数);
--(*项目计数);
printf(“记录已删除。\n”);
}else//删除第一条或中间记录
{
对于(i=索引;i<*itemcount-1;i++){
st[i]=st[i+1];
清洁(st,*itemcount);
--(*项目计数);
}
}
}否则
printf(“记录不存在。请检查ID并重试。\n”);
}否则
printf(“无需删除的记录”);
}
//清除已删除记录的函数
无效的
清洁(学生st[],内部索引){
strcpy(st[index].stnumber,“”);
strcpy(st[index].stname,“”);
st[index].sex=NULL;
st[index].quizz1=0;
st[index].quizz2=0;
st[index].assignment=0;
st[指数]。中期=0;
st[index].final=0;
st[指数]。总计=0;
}
index
(从
search()
返回的值)在搜索中设置为记录的实际循环索引,其中
st[i].stnumber
id
之间的字符串比较匹配:

if (strcmp (st[i].stnumber, id) == 0)
    found = i;
. . .
return found;

这确保
索引
具有列表中记录的
索引
的数值。从
search()
返回的不是
1
-1

当发布到StackOverflow时,请确保包含您有疑问的实际代码,而不是链接。这里很少有人会像我一样去为你获取代码。看看张贴要求,他们在这里是有原因的。祝你好运。
clean(st,*itemcount)--(*项目计数):错误。而
&(*itemcount!=0)
是不必要的。
if (strcmp (st[i].stnumber, id) == 0)
    found = i;
. . .
return found;