简体   繁体   中英

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

This is for schoolwork and the question is asking to make a programme like a student database where I am able to input, print and remove students from the structure s using pointers function. However I am unable to delete the student records as something weird happens. The target student gets deleted but the rest of the students records(names only) are shifted, with only the first character being correct. Please help!

#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 = '\0';
                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 = '\0';
        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=='\0') (s+j)->id = '\0';
            }
            return 0;
        }
    }
    return 2;
}

you were almost there.

The problem is at line 97: *((s+j)->name) = *((s+k)->name);

To make it work, this instruction should be substituted with:

strcpy((s+j)->name, (s+k)->name);

Why:

you are coping with char arrays, so doing *((s+j)->name) = *((s+k)->name) means: "put the value of the first char of s+k->name into s+j->name first char". Instead, you want to copy the whole name of s+k into the s+j name.

on how to use strcpy you can take a look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM