c++ - 创建结构实例数组,然后在传递对象数组(指针)的其他结构中调用函数

标签 c++ arrays pointers struct

我已经发布了一个与我在大学考试中可能遇到的问题类似的问题,现在这是我面临的另一个具体问题,可能是由于缺乏对指针的重要理解。

这个问题定义了几个struct

其中之一是 struct Question{},它具有指针属性,还有一个数组,用于保存针对该特定问题给出的所有答案。在目的地,我应该能够遍历所有问题,以便将它们一一显示给用户。

当我实例化考试时(这是模拟入学考试),我需要传递学生的公民身份证号码和考试问题。

// pi._prijavljeniKandidati[1]->_JMBG is the ID number in question
// 'questions' is supposed to carry all the questions I've hard-coded
// to save myself from entering manually

pi.StartExam(pi._prijavljeniKandidati[1]->_JMBG, questions);

我是这样尝试的:

Question* questions = new Question;

// this initializes a single question
// 'answers' is the attribute that is holding all the answers
// the correct answer is BTW determined by an integer that is also
// sent in the below function

char* answers1[4];
answers1[0] = "London";
answers1[1] = "Berlin";
answers1[2] = "Helsinki";
answers1[3] = "Rome";

questions[0].Create("What is the capital of Finland?", answers1, 2);

// another question
char* answers2[3];
answers2[0] = "Ljubljana";
answers2[1] = "Paris";
answers2[2] = "Prague";

questions[0].Create("What is the capital of France?", answers2, 1);

这就是 StartExam 函数的实际样子,这里没有什么特别的,只是它显示了我如何尝试获取某些问题的某些值(基于其索引):

// I also tried void PokreniIspit(char* ID, Question* questions[])
void StartExam(char* ID, Question* questions)
{
    // this is just some dummy code line, to make sure it works
    cout << questions[1]._txtOfQuestion << endl;
}

当我运行应用程序时,控制台崩溃了。有什么明显的东西会导致它崩溃吗?

为了完整起见,这里是整个问题结构:

// THIS IS HOW I IMAGING THIS STRUCT 'VISUALLY'
//= _txtOfQuestion ["Koji je glavni grad Njemacke?"]
//= _answers[10] //max 10 answers
//==== [0] Peking
//==== [1] London
//==== [2] Berlin
//==== [3] Seattle
//==== [4] Ljubljana
//= _posOfCorrect [2]
//= _points [4]

struct Question{

    char* _txtOfQuestion;
    char* _answers[4];
    int _posOfCorrect;
    int _points;

    void Unos(char* txt, char* answers[], int posCorrect, int points)
    {
        _txtOfQuestion= new char[strlen(txt) + 1];
        strcpy_s(_txtOfQuestion, strlen(txt) + 1, txt);

        for(int i = 0; i < 4; i++){
            _answers[i] = new char;
            strcpy_s(_answers[i], strlen(_answers[i]) + 1, _answers[i]);
        }

        _posOfCorrect = posCorrect;
        _points = points;

}

最佳答案

感谢大家的帮助。发布这个问题并检查所有这些代码以将波斯尼亚语翻译成英语帮助我注意到出了什么问题。

此外,由于我确实最初尝试像这样初始化Question:

Question* questions = new Question[2];

我又开始使用它了,因为我确实需要一系列问题。

但真正的罪魁祸首(也是导致控制台崩溃的原因)是我在 for 循环中硬编码了 4

当我让我的第二个问题包含 4 个选项/答案时,就像第一个问题一样 - 它奏效了。

for (int i = 0; i < 4; i++){
    _odgovori[i] = new char;
    strcpy_s(_odgovori[i], strlen(odgovori[i]) + 1, odgovori[i]);
}

关于c++ - 创建结构实例数组,然后在传递对象数组(指针)的其他结构中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31992207/

相关文章:

c++ - 根据执行位置自动填充成员函数参数的优雅方法

c++ - 从 C 字符串数组创建 string_views 的范围

c++ - C++ 设置比较器上的 Visual Studio 断言失败

javascript - 如何在数组中添加数字

java - 初始化 int 数组

c - 如何打印数组中元素的数量?

c++ - 将函数指针返回到在 main() 中初始化的对象

c++如何使两个 vector 一个带有数据另一个指向并且只读

c++ - 在 C++ 中使用集合迭代器

c - 使用通配符 (*) 作为第一个输入时,程序出现段错误