在其他函数中使用realloc后无法访问数组中的数据

问题描述 投票:0回答:1

在课堂上,我必须编写使用结构数组的简单程序,该程序需要添加元素并显示一个或所有元素。老师说不能用向量,必须用指针,所以我不能用

array[0] = x
x.name
之类的东西。

问题是我无法在 add_element 函数中访问数组,并且从其他函数返回或进入其他函数后我无法访问它。据我发现问题与 realloc 有关,但我无法弄清楚。

我有两天时间将其发送给老师,请帮忙。

#include <iostream>
#include <stdlib.h>
#include <string>
#include <unistd.h>

using namespace std;

struct student {
    string name, last_name, date_of_birth;
    unsigned int index;
    char gender;
};

void add_element(student tab[], int &size) {
    int new_size = size + 1;
    tab = (student*)realloc(tab, sizeof(student) * new_size);
    student *x = new student;
    cout << "set name: ";
    cin >> x -> name;
    cout << "set last name: ";
    cin >> x -> last_name;
    cout << "set date of birth: ";
    cin >> x -> date_of_birth;
    cout << "set gender: ";
    cin >> x -> gender;
    cout << "set index: ";
    cin >> x -> index;
    tab[size] = *x;
    size = new_size;
    cout << tab -> name << endl; //here i can access
    delete x;
}

void display(student tab[], int *size) {
    for (int i = 0; i < *size; i++)
        cout << (tab + i) -> name << ' ' << (tab + i) -> last_name << ", " << (tab + i) -> date_of_birth << ", " << (tab + i) -> gender << ", " << (tab + i) -> index << endl;
}
void display(student tab[], int *size, int i) {
    if (i < *size) {
        cout << (tab + i) -> name << ' ' << (tab + i) -> last_name << ", " << (tab + i) -> date_of_birth << ", " << (tab + i) -> gender << ", " << (tab + i) -> index << endl;
    }
}

int main() {
    int size = 0;
    student* students = (student*)malloc(sizeof(student) * size);
    int option;
    do {
        cout << "\x1B[2J\x1B[H";
        cout << "choose what to do\n";
        cout << "0 - add element\n1 - display all elements\n2 - display specific element\n3 - exit\n";
        cin >> option;
        switch (option) {
            case 0:
                add_element(students, size);
                cout << students -> name << endl; //after leaving function can't access
                sleep(2);
            break;
            case 1:
                display(students, &size);
                sleep(2);
            break;
            case 2:
                cout << "provide index for element to display\n";
                int n;
                cin >> n;
                display(students, &size, n);
                sleep(2);
            break;
            case 3:
            break;
            default:
                cout << "option doesn't exist\n";
                sleep(2);
        }
    }
    while (option != 3);
    delete [] students;
    return 0;
}

我尝试更改将数组从引用传递到函数中的方式到指针,但除了一些不会改变任何内容的小更改。

c++ arrays pointers struct
1个回答
0
投票

您已经为

size
完成的最快方法如下;变化:

void add_element(student tab[], int &size)

至:

void add_element(student &tab[], int &size)
//                       ^ A reference to an array

另外,我个人会选择:

student *x = new student;

用途:

student x;

当然使用比

x
更好的变量名。将结构体定义为
struct Student {...}
然后有
Student student;

© www.soinside.com 2019 - 2024. All rights reserved.