C编程调用引用指针函数删除结构成员

C programming call by reference pointer function to delete a member of a structure

这是关于功课的,问题是要求制作一个像学生数据库这样的程序,我可以在其中使用指针函数从结构中输入、打印和删除学生。但是,由于发生了一些奇怪的事情,我无法删除学生记录。目标学生被删除,但其余学生记录(仅姓名)被移动,只有第一个字符是正确的。请帮忙!

#include <stdio.h>
#include <string.h>
#define SIZE 50

typedef struct {
    int id;
    char name[50];
} Student;

void inputStud(Student *s, int size);
void printStud(Student *s, int size);
int removeStud(Student *s, int *size, char *target);
int main()
{
    Student s[SIZE];
    int size=0, choice;
    char target[80], *p;
    int result;

    printf("Select one of the following options: \n");
    printf("1: inputStud()\n");
    printf("2: removeStud()\n");
    printf("3: printStud()\n");
    printf("4: exit()\n");
    do {
        printf("Enter your choice: \n");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                printf("Enter size: \n");
                scanf("%d", &size);
                printf("Enter %d students: \n", size);
                inputStud(s, size);
                break;
            case 2:
                printf("Enter name to be removed: \n");
                scanf("\n");
                fgets(target, 80, stdin);
                if (p=strchr(target,'\n')) *p = '[=10=]';
                printf("removeStud(): ");
                result = removeStud(s, &size, target);
                if (result == 0)
                    printf("Successfully removed\n");
                else if (result == 1)
                    printf("Array is empty\n");
                else if (result == 2)
                    printf("The target does not exist\n");
                else
                    printf("An error has occurred\n");
                break;
            case 3:
                printStud(s, size);
                break;
        }
    } while (choice < 4);
    return 0;
}

void inputStud(Student *s, int size)
{
    int i=0;
    char *p,dummy[50];
    while (i<size) {
        printf("Student ID: \n");
        scanf("%d",&((s+i)->id));
        printf("Student Name: \n");
        scanf("\n");
        fgets((s+i)->name, 50,stdin);
        if (p=strchr((s+i)->name,'\n')) *p = '[=10=]';
        i++;
    }
}

void printStud(Student *s, int size)
{
    int i;
    printf("The current student list: \n");
    if (size==0) printf("Empty array\n");
    else {
        for (i=0; i<size; i++) {
            printf("Student ID: %d ",(s+i)->id);
            printf("Student Name: %s\n",(s+i)->name);
        }
    }
}

int removeStud(Student *s, int *size, char *target)
{
    int i,j,k;

    if (*size==0) return 1;
    for (i=0;i<*size;i++) {
        if (strcmp(((s+i)->name),target)==0) {
            --*size;
            for (j=i; j<=*size; j++) {
                k = j + 1;
                *((s+j)->name) = *((s+k)->name);
                (s+j)->id = (s+j+1)->id;
                if ((s+j+1)->id=='[=10=]') (s+j)->id = '[=10=]';
            }
            return 0;
        }
    }
    return 2;
}

你快到了。

问题出在第 97 行:*((s+j)->name) = *((s+k)->name);

要使其正常工作,应将此指令替换为:

strcpy((s+j)->姓名, (s+k)->姓名);

为什么:

你正在处理 char 数组,所以做 *((s+j)->name) = *((s+k)->name) 意味着:“把 s+ 的第一个字符的值k->name into s+j->name first char”。 相反,您想将 s+k 的整个名称复制到 s+j 名称中。

关于strcpy的使用方法可以看看here