【问题标题】:Dynamically allocating arrays of structures of arrays动态分配数组结构的数组
【发布时间】:2020-01-19 13:52:51
【问题描述】:

您好,很抱歉再次提出,我可以看到这里有很多类似的问题,但我仍然无法理解我做错了什么。 我的任务是为学生信息编写一个简单的数据库,具有动态内存分配和结构。 我的问题是我不明白如何创建动态分配的数组,在其上写入信息以及如何进一步传递它以读取它并在另一个函数的屏幕上显示。 我尝试编写一些代码,它甚至可以工作,但是一天过去了,我尝试运行它并出现了问题,编译器没有显示任何重大问题,但它只是在输入第一个值(学生 ID)后停止。 请帮我解决我做错了什么,我不知道我该怎么做。

这是我的代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>




struct student{
    char ID[7];
    char name[20];
    char surename[20];
    char addres[20];
    char phone_num[9];
};


//inputs with arrays overflow protection
student new_student(int k)
{
    student *st,*stt;
    if(k>0)
    {
        realloc(st,k*sizeof(student));
    }
    else
    {
        st=(student*)malloc(sizeof(student));
        if (st==NULL)
        {
            printf("\nERROR\nnot enough memory");
            exit(1);
        }
    }


    system("cls");

    //ID
    while (1)
    {
        fflush(stdin);
        printf("\nenter 7 digit student ID: ");
        gets(st->ID);



        //check the entered ID
        if (st->ID[7]!='\0')
        {
            printf("\nentered ID is to long\n");
            getch();
            system("cls");
            st->ID[7]='\0';
        }
        else if(st->ID[6]=='\0')
        {
            printf("\nentered ID is to short\n");
            getch();
            system("cls");
            st->ID[6]='\0';
        }
        else
        {
            break;
        }       
    }

    //NAME
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's name: ");
        gets(st->name);
        if (st->name[20]!='\0')
        {
            printf("\nentered name is to long\n");
            getch();
            system("cls");
            st->name[20]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st->ID[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }

    //SURENAME
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's surename: ");
        gets(st->surename);
        if (st->surename[20]!='\0')
        {
            printf("\nentered surename is to long\n");
            getch();
            system("cls");
            st->surename[20]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st->ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st->name[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }
    //ADDRES
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's addres: ");
        gets(st->addres);
        if (st->addres[20]!='\0')
        {
            printf("\nentered addres is to long\n");
            getch();
            system("cls");
            st->addres[20]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st->ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st->name[i]);
            printf("\n");
            printf("\nenter student's surename: ");
            for (int i=0;i<20;i++)
                printf("%c",st->surename[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }
    //PHONE
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's phone number: ");
        gets(st->phone_num);

        if (st->phone_num[9]!='\0')
        {
            printf("\nentered phone number is to long\n");
            getch();
            system("cls");
            st->phone_num[9]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st->ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st->name[i]);
            printf("\n");
            printf("\nenter student's surename: ");
            for (int i=0;i<20;i++)
                printf("%c",st->surename[i]);
            printf("\n");
            printf("\nenter student's addres: ");
            for (int i=0;i<20;i++)
                printf("%c",st->addres[i]);
            printf("\n");
        }

        else if(st->phone_num[8]=='\0')
        {
            printf("\nentered phone number is to short\n");
            getch();
            system("cls");
            st->phone_num[8]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st->ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st->name[i]);
            printf("\n");
            printf("\nenter student's surename: ");
            for (int i=0;i<20;i++)
                printf("%c",st->surename[i]);
            printf("\n");
            printf("\nenter student's addres: ");
            for (int i=0;i<20;i++)
                printf("%c",st->addres[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }


    return *st;
}




void search_student ()
{
    ;
}

int main(int argc, char** argv)
{
    while (1)
    {   
    system("cls");
    int choice,st_counter=0;
    printf("===MENU===\n");
    printf("1. Add Student\n");
    printf("2. Search Student\n");
    printf("3. Modify Student Record\n");
    printf("4. Generate Marksheet\n");
    printf("5. Delete Student Record\n");
    printf("6. Exit\n");
    printf("Enter your choice: ");
    scanf("%d",&choice);

     switch(choice)
     {
            case 1:
                st_counter++;//increase array size every time entering new student's data
                new_student(st_counter);                
                break;
            case 2:
                void search_student ();
                break;
            case 3:

                break;
            case 4:
                //this will print to file later
                break;
            case 5:

                break;
            case 6:
                return 0;
                break;
            default:
                break;
     }
    }

        return 1;
}


【问题讨论】:

  • if(k&gt;0) {realloc(st,k*sizeof(student)); 这期望st 指向一个使用malloc 或类似分配的有效内存块。实际上,st 是一个包含随机垃圾的未初始化变量。因此您的程序表现出未定义的行为。
  • 如果您的代码是 C++ 而不是 C,请使用 std::vector
  • 您可以使用std::string 代替char[]
  • 您的 C 风格字符串有问题。当您说char ID[7]; 时,该数组有 7 个元素,唯一有效的索引是 0-6。由于 C 字符串以 NULL 结尾,因此只有 6 个字符和 NULL 的空间。您不能将 7 位 id 放入该数组,也不能编写像 if (st-&gt;ID[7]!='\0') 这样的代码,因为 7 不是有效索引。您的所有其他字符串也有同样的问题。停止这样做,只使用std::string(和std::vector)。
  • 选择一种语言,C 或 C++,并删除另一种的标记。

标签: c arrays structure


【解决方案1】:

好吧,考虑到您写的内容,我稍微更改了代码。我希望它更接近正确。

student new_student(int k)
{
    student *st;
    if(k>1)
    {
        realloc(st,k*sizeof(student));
    }
    else
    {
        st=(student*)malloc(sizeof(student));
        if (st==NULL)
        {
            printf("\nERROR\nnot enough memory");
            exit(1);
        }
    }


    system("cls");

    k-=1;
    //ID
    while (1)
    {
        fflush(stdin);
        st[k].ID[6]='\0';
        st[k].ID[7]='\0';
        printf("\nenter 7 digit student ID: ");
        gets(st[k].ID);



        //check the entered ID
        if (st[k].ID[7]!='\0')
        {
            printf("\nentered ID is to long\n");
            getch();
            system("cls");
            st[k].ID[7]='\0';
        }
        else if(st[k].ID[6]=='\0')
        {
            printf("\nentered ID is to short\n");
            getch();
            system("cls");
            st[k].ID[6]='\0';
        }
        else
        {
            break;
        }       
    }

    //NAME
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's name: ");
        gets(st[k].name);
        if (st[k].name[20]!='\0')
        {
            printf("\nentered name is to long\n");
            getch();
            system("cls");
            st[k].name[20]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st[k].ID[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }

    //SURENAME
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's surename: ");
        gets(st[k].surename);
        if (st[k].surename[20]!='\0')
        {
            printf("\nentered surename is to long\n");
            getch();
            system("cls");
            st[k].surename[20]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st[k].ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].name[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }
    //ADDRES
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's addres: ");
        gets(st[k].addres);
        if (st[k].addres[20]!='\0')
        {
            printf("\nentered addres is to long\n");
            getch();
            system("cls");
            st[k].addres[20]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st[k].ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].name[i]);
            printf("\n");
            printf("\nenter student's surename: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].surename[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }
    //PHONE
    while (1)
    {
        fflush(stdin);
        printf("\nenter student's phone number: ");
        gets(st[k].phone_num);

        if (st[k].phone_num[9]!='\0')
        {
            printf("\nentered phone number is to long\n");
            getch();
            system("cls");
            st[k].phone_num[9]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st[k].ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].name[i]);
            printf("\n");
            printf("\nenter student's surename: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].surename[i]);
            printf("\n");
            printf("\nenter student's addres: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].addres[i]);
            printf("\n");
        }

        else if(st[k].phone_num[8]=='\0')
        {
            printf("\nentered phone number is to short\n");
            getch();
            system("cls");
            st[k].phone_num[8]='\0';

            printf("\nenter 7 digit student ID: ");
            for (int i=0;i<7;i++)
                printf("%c",st[k].ID[i]);
            printf("\n");           
            printf("\nenter student's name: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].name[i]);
            printf("\n");
            printf("\nenter student's surename: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].surename[i]);
            printf("\n");
            printf("\nenter student's addres: ");
            for (int i=0;i<20;i++)
                printf("%c",st[k].addres[i]);
            printf("\n");
        }
        else
        {
            break;
        }
    }


    return *st;
}

【讨论】:

    猜你喜欢
    • 2019-07-19
    • 2020-03-06
    • 2021-10-30
    • 2017-03-30
    • 1970-01-01
    • 2012-03-25
    • 2011-12-13
    相关资源
    最近更新 更多