动态数组的结构,delet元素

时间:2017-04-03 11:32:39

标签: c pointers dynamic-arrays

我正在尝试为学生创建一个数据库程序,您应该能够添加/修改和删除学生。我设法让add函数工作,还有修改函数,但删除函数给了我一些问题。当我试图从数据库中删除一名学生时,我的代码似乎崩溃了,有人能告诉我问题所在吗?

这是我的代码:

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* TODO: Avoid global variables. */

struct student {
    char name[60];
    long long personalNumber;
    char gender[6];
    char studyProgram[60];
    char email[30];
    int age;
};

struct student *pointer = NULL;
int numberofstudents = 0;

void modify()
{
    long long persnr;
    long long comp;
    int match = 0;
    printf("Please enter the personal number that you wish to modify: \n");
    scanf("%lld", &persnr);
    getchar();

    for(int i = 0; i <= numberofstudents; i++)
    {
        comp = ((pointer+i)->personalNumber);
        printf("%lld\n", ((pointer+i)->personalNumber));
        printf("%lld\n", comp);


        printf("Inne");
        if (pointer[i].personalNumber == persnr && match == 0)
        {
            printf("Enter name, personalnumber, gender, studyprogram, email and age in this order\n");
            scanf("%s%lld%s%s%s%d", (pointer+i)->name, &(pointer+i)->personalNumber, (pointer+i)->gender, (pointer+i)->studyProgram, (pointer+i)->email, &(pointer+i)->age);
            match = 1;
            getchar();
        }
       if (match == 0)
       {
           printf("Could not find person");
       }

    }
}


void deletestudent()
{
    long long persnr;
    long long comp;
    int match = 0;
    printf("Please enter the personal number that you wish to delete: \n");
    scanf("%lld", &persnr);
    getchar();

    struct student *temporary = malloc((numberofstudents - 1) * sizeof(struct student));

    for(int i = 0; i <= numberofstudents; i++)
    {
        if (pointer[i].personalNumber == persnr && match == 0)
        {
            match = 1;
        }
        else if (match == 1){
            temporary[i-1] = pointer[i];
        }
        else
        {
            temporary[i] = pointer[i];
        }

       if (match == 0)
       {
           printf("Could not find person");
       }

    }
    free(pointer);
    pointer = temporary;

    }



void add(){

        if (numberofstudents > 0)
        {
            pointer = (struct student*) realloc(pointer, (numberofstudents+1) * sizeof(struct student));

            printf("Lyckades allokeringen!\n\n");
        }

        printf("Enter name, personalnumber, gender, studyprogram, email and age in this order\n");
        scanf("%s%lld%s%s%s%d", (pointer+numberofstudents)->name, &(pointer+numberofstudents)->personalNumber, (pointer+numberofstudents)->gender, (pointer+numberofstudents)->studyProgram, (pointer+numberofstudents)->email, &(pointer+numberofstudents)->age);
        getchar();
        printf("Visar data:\n");

        for(int i = 0; i <= numberofstudents; ++i)
        {
            printf("%s\t%lld\t%s\t%s\t%s\t%d\n", (pointer+i)->name, (pointer+i)->personalNumber, (pointer+i)->gender, (pointer+i)->studyProgram, (pointer+i)->email, (pointer+i)->age);
        }
        numberofstudents = numberofstudents+1;
    }

int main(void)
{
    pointer = (struct student*) malloc(2 * sizeof(struct student));

    if (pointer == NULL)
{
    printf("pointer NULL");
    exit(1);
}

    int run = 1;
    int choice;

    while (run == 1)
    {
        printf("Please enter an option listed below\n1.ADD\n2.Modify\n3.Delete\n4.Search\n5.Save\n6.Load\n7.Exit");
        scanf("%d", &choice);
        getchar();

        switch(choice) {

   case 1 :
      add();
      break;

   case 2 :
      modify();
      break;
   case 3 :
      deletestudent();


   case 7 :
      exit(0);
      break;

    default :

      break;
}

    }


    return 0;
}

1 个答案:

答案 0 :(得分:0)

如评论中所述,这:

for(int i = 0; i <= numberofstudents; i++)

是一个巨大的警示标志。在C中,这样的循环通常应该包含i < numberofstudents,假设numberofstudents是数组的实际长度。

使用<=而不是<会使循环走得太远,从而在数组外部进行索引并导致未定义的行为。

写入堆分配的内存,然后尝试free()这是引发崩溃的好方法,因为你有可能踩到堆分配器的数据结构(实际上;理论上所发生的一切)是你得到未定义的行为,因此任何事情都可能发生。)