【问题标题】:figure out pointers and structs找出指针和结构
【发布时间】:2018-09-09 07:40:13
【问题描述】:

我一直在试图弄清楚如何处理指针和结构。我写了以下代码。

#include <iostream>

using namespace std;

struct Person
{
    char name[20]; //Question 2
    int id;
};

const int max_num_of_childs=10;

struct Family
{
    Person dad;
    Person mom;
    int num_of_childs;
    Person* child[max_num_of_childs];
};

void add_child (Family& f)
{
    char answer;
    do
    {
        if (f.num_of_childs==max_num_of_childs)
        {
            cout << "no more children" <<endl;
            return;
        }

        cout << "more child? Y/N" <<endl;
        cin >> answer;
        if (answer == 'Y')
        {
            f.child[f.num_of_childs] = new Person;
            cout << "enter name and id" << endl;
            cin >> f.child[f.num_of_childs]->name;
            cin >> f.child[f.num_of_childs]->id;
            f.num_of_childs++;
        }
    }
    while (answer=='Y');

    return;
}

void add (Family& f)
{
    cout << "insert dad name & id" << endl;
    cin >> f.dad.name >> f.dad.id;

    cout << "\ninsert mom name & id" << endl;
    cin >> f.mom.name >> f.mom.id;

    add_child (f);
}

void print_child (const Family f) //Question 1
{
    for (int i=0; i<f.num_of_childs; i++)
        cout << "#" << i+1 << "child name: " << f.child[f.num_of_childs]->name << "child id: " << f.child[f.num_of_childs]->id << endl;
}

void print (const Family f)
{
    cout << "dad name: " << f.dad.name << "\tdad id: " << f.dad.id << endl;
    cout << "mom name: " << f.mom.name << "\tmom id: " << f.mom.id << endl;
    print_child (f);
}

int main()
{
    Family f;
    f.num_of_childs=0;
    add(f);
    print(f);

    return 0;
}

为什么print_child() 的输出是乱码?

爸爸姓名:AAAA 爸爸身份证:11 妈妈姓名:BBBB 妈妈编号:22 1个孩子姓名:ïUï∞u u u h░├evhchild id:6846053 2个孩子姓名:ïUï∞u u u h░├evhchild id:6846053

如何定义一个无限长的字符数组? (使用字符串也需要定义长度)。

【问题讨论】:

  • 请提取minimal reproducible example。尤其要消除示例代码中的输入,因为总是输入输入既不方便也不安全。
  • 为什么每个家庭都有一个mum和一个dad,顺便说一句?
  • @UlrichEckhardt 我们正在根据政治正确性来评判玩具练习?
  • print_child 中你需要使用f.child[i],而不是f.child[f_num_of_childs]
  • 现实的树可能是hard,见Cycles in family tree software :-)

标签: c++ c++11


【解决方案1】:

为什么print_child()的输出是乱码?

print_child() 方法中,代码超出了f.child 数组的初始化范围。有:

void print_child (const Family f) //Question 1
{
    for (int i=0; i<f.num_of_childs; i++)
        cout << "#" << i+1 << "child name: " << f.child[f.num_of_childs]->name << "child id: " << f.child[f.num_of_childs]->id << endl;
}

我认为应该有:

void print_child (const Family f) //Question 1
{
    for (int i=0; i<f.num_of_childs; i++)
        cout << "#" << i+1 << "child name: " << f.child[i]->name << "child id: " << f.child[i]->id << endl;
}

i 总是小于f.num_of_childs,所以代码不会到达未初始化的内存。

除此之外,还有一件小事。

通常整数类成员被初始化为 0,但这并不能保证。我建议初始化,以确保在创建 Family 类的对象时 num_of_childs 的初始值为 0:

struct Family
{
    Person dad;
    Person mom;
    int num_of_childs = 0;
    Person* child[max_num_of_childs];
};

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2015-07-10
    • 2013-02-15
    • 2023-04-04
    • 2023-03-15
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多