将结构插入动态数组中

时间:2022-09-06 11:27:00

i am new c-programmer. I am trying to write a small student-database, using the concept of struct combined with the dynamic memory (malloc, free...). So I created a struct student and i allocated a dynamic array of fixed size.

我是新的程序员。我正在尝试编写一个小型学生数据库,使用struct的概念结合动态内存(malloc,free ...)。所以我创建了一个struct student,并分配了一个固定大小的动态数组。

But I have a problem. When i am inserting new students and then printing the database I get an array filled only with the last entry.

但我有一个问题。当我插入新学生然后打印数据库时,我得到一个只填充最后一个条目的数组。

Ex:

insert(a1,a2,1)
insert( b1,b2,2)
-> so i get the following:
   first-student: b1, b2, 1
   second-student: b1, b2,2

I habe probably a mistake in the inserting-function.

我在插入函数中可能是个错误。

The display()-Function is normally working fine. I could not find the problem

显示() - 功能通常正常。我找不到问题

this is the code:

这是代码:

    #include <stdlib.h>
    #include <stdio.h>
    struct student{
        char * lastname;
        char * firstname;
        int  mNr;
    };

    struct student * db;
    struct student * firstElement;

    void createDb(){
        db= (struct student *)malloc(3*sizeof(struct student *));
        firstElement=db;
        printf("database was created\n");
    }
    struct student getData(char lastname [], char firstname[], int matNr){
        struct student st;
        st.lastname=lastname;
        st.firstname=firstname;
        st.mNr=matNr;
        printf("%s,%s,%d\n",st.lastname,st.firstname,st.mNr);
        return st;
   }

    void insert_student(struct student *  st){
        *db=*st;
        db++;
    }
    void delete_student(int immaNr){

    }
    void search_student(int immaNr){
    }
    void display(){
        db=firstElement;
        int i;
        for(i=0;i<3;i++){
            printf("student %d:\n",i+1);
            printf("lastname: %s, firstname:%s, enrollment nr:%d\n",db->lastname,db->firstname,db->mNr);
            db++;
        }
     }
    int main(int argc, char ** argv){

    char  lastname[7];
    char  firstname[7];
    int matNr;
    int operation;
    createDb();
    printf("===================================\n");
    printf("   WELCOME TO STUDENT DATABASE     \n");
    printf("===================================\n");
    printf("\n");
    begin:      
        operation=0;
        printf("please choose one of these operations:\n");
        printf("   (1) enter a new student\n");
        printf("   (2) delete a student\n");
        printf("   (3) search a student\n");
        printf("   (4) show the current database\n");
        printf("   (5) exit\n");
        scanf("%d",&operation);
        switch(operation){
            case 1:     
                printf("enter the lastname\n");
                scanf("%s", lastname);
                printf("enter the firstname\n");
                scanf("%s", firstname);
                printf("enter the matriculation number:%d\n",matNr);
                scanf("%d",&matNr);
                struct student s=getData(lastname,firstname,matNr);
                insert_student(&s);
                matNr=0;
                goto begin;
            case 4:
                display();
                goto begin;
            case 5:
                puts("exit");
                exit(0);
        }
}

1 个解决方案

#1


Ok, this is what's happening here. In your struct

好的,这就是这里发生的事情。在你的结构中

struct student{
    char * lastname;
    char * firstname;
    int  mNr;
};

You are storing pointers to character arrays rather than character arrays, this is fine if you are malloc'ing them everytime you create a new student, and do a strcpy() or memcpy() to copy the input into the struct.

您正在存储指向字符数组而不是字符数组的指针,如果您每次创建新学生时都要对它们进行malloc'ing,并执行strcpy()或memcpy()将输入复制到结构中,则可以。

But because you are just copying the input by reference all your student instances end up pointing to the last input firstname/lastname. Change your struct as so

但是因为您只是通过引用复制输入,所有学生实例最终都指向最后输入的firstname / lastname。像这样改变你的结构

 struct student{
    char  lastname[20];
    char  firstname[20];
    int  mNr;
};

And use memcpy(st.lastname,lastname,strlen(lastname)) for lastname and firstname in your 'getData' method.

并使用memcpy(st.lastname,lastname,strlen(lastname))作为'getData'方法中的lastname和firstname。

Or keep your current implementation of the struct and change your getdata as so

或者保持结构的当前实现并更改你的getdata

 struct student getData(char lastname [], char firstname[], int matNr){
    struct student st;
    st.lastname=malloc(strlen(lastname));
    memcpy(st.lastname,lastname,strlen(lastname))
    st.firstname=malloc(strlen(firstname));
    memcpy(st.firstname,firstname,strlen(firstname))
    st.mNr=matNr;
    printf("%s,%s,%d\n",st.lastname,st.firstname,st.mNr);
    return st;
 } 

#1


Ok, this is what's happening here. In your struct

好的,这就是这里发生的事情。在你的结构中

struct student{
    char * lastname;
    char * firstname;
    int  mNr;
};

You are storing pointers to character arrays rather than character arrays, this is fine if you are malloc'ing them everytime you create a new student, and do a strcpy() or memcpy() to copy the input into the struct.

您正在存储指向字符数组而不是字符数组的指针,如果您每次创建新学生时都要对它们进行malloc'ing,并执行strcpy()或memcpy()将输入复制到结构中,则可以。

But because you are just copying the input by reference all your student instances end up pointing to the last input firstname/lastname. Change your struct as so

但是因为您只是通过引用复制输入,所有学生实例最终都指向最后输入的firstname / lastname。像这样改变你的结构

 struct student{
    char  lastname[20];
    char  firstname[20];
    int  mNr;
};

And use memcpy(st.lastname,lastname,strlen(lastname)) for lastname and firstname in your 'getData' method.

并使用memcpy(st.lastname,lastname,strlen(lastname))作为'getData'方法中的lastname和firstname。

Or keep your current implementation of the struct and change your getdata as so

或者保持结构的当前实现并更改你的getdata

 struct student getData(char lastname [], char firstname[], int matNr){
    struct student st;
    st.lastname=malloc(strlen(lastname));
    memcpy(st.lastname,lastname,strlen(lastname))
    st.firstname=malloc(strlen(firstname));
    memcpy(st.firstname,firstname,strlen(firstname))
    st.mNr=matNr;
    printf("%s,%s,%d\n",st.lastname,st.firstname,st.mNr);
    return st;
 } 

相关文章