【问题标题】:Why has my program stopped working? Are my pointers wrong?为什么我的程序停止工作?我的指针错了吗?
【发布时间】:2011-04-12 01:49:00
【问题描述】:

我正在开发一个程序,该程序从格式如下的文件中读取一些数据:

21
285 270 272 126 160 103 1
31 
198 180 163 89 94 47 1 
32
240 230 208 179 163 104 1
33
15 13 12 14 15 15 0
34
63 61 62 24 23 20 2

我试图将第一个数字读入一个指针数组,将其他 7 个数字读入一个并行的二维指针数组,但由于某种原因,每次我运行我的代码时它都会停止工作。它没有返回错误,但我觉得我的指针使用错误,因为这是我第一次使用指针。数据文件名为“election_data_121.txt”,仅供参考。这是代码。如果有人可以看一下,我将不胜感激:

#include <iostream>
#include <fstream>
using namespace std;

//bool openFileIn(fstream &, char *);

int main()
{
    const int PREC_SIZE = 30;
    const int CANIDATES = 7;
    int *precinct_num[PREC_SIZE];
    int *num_votes[PREC_SIZE][CANIDATES];
    cout << "Declarations made." << endl;

    fstream dataFile; //Make a file handle
    cout << "File object made." << endl;
    //Open the file and check that it opened correctly
    if(!openFileIn(dataFile, "election_data_121.txt"))
    {
        cout << "File open error!" << endl;
        return 0; //Exit the program
    }

    cout << "File opened." << endl;

    //Read the contents of the file into the proper arrays
    int counter = 0;
    while(!dataFile.eof()) 
    {
        dataFile >> *precinct_num[counter];
        for(int i = 0; i < 7; i++)
        {
            dataFile >> *num_votes[counter][i];
        }
        counter++;
    }

    //Print out the data
    for(int j = 0; j < counter; j++)
    {
        cout << *precinct_num[j];
        for(int i = 0; i < 7; i++)
        {
            cout << *num_votes[j][i];
        }
    }

    dataFile.close();
    cout << "End of file";
    return 0;
}

bool openFileIn(fstream &file, char *name)
{
    file.open(name, ios::in);
    if(file.fail())
        return false;
    else
        return true;
}

再次感谢!

【问题讨论】:

  • 您遇到的具体问题是什么?
  • 我认为您不太了解指针是什么。如果您想弄清楚指针是什么以及它们的作用,那么一个关于指针的好教程可能会有很长的路要走。
  • 这有点像超高端商品的价格:如果你要问,他们就错了。有了指针你负责知道。

标签: c++ pointers


【解决方案1】:

这段代码根本不需要指针;为什么你会认为它会呢?只需更改 precinct_num 和 num_votes 的类型并停止取消引用它们,我认为(一目了然)应该没问题。

#include <iostream>
#include <fstream>
using namespace std;

//bool openFileIn(fstream &, char *);

int main()
{
    const int PREC_SIZE = 30;
    const int CANIDATES = 7;
    int precinct_num[PREC_SIZE];
    int num_votes[PREC_SIZE][CANIDATES];
    cout << "Declarations made." << endl;

    fstream dataFile; //Make a file handle
    cout << "File object made." << endl;
    //Open the file and check that it opened correctly
    if(!openFileIn(dataFile, "election_data_121.txt"))
    {
        cout << "File open error!" << endl;
        return 0; //Exit the program
    }

    cout << "File opened." << endl;

    //Read the contents of the file into the proper arrays
    int counter = 0;
    while(!dataFile.eof()) 
    {
        dataFile >> precinct_num[counter];
        for(int i = 0; i < 7; i++)
        {
            dataFile >> num_votes[counter][i];
        }
        counter++;
    }

    //Print out the data
    for(int j = 0; j < counter; j++)
    {
        cout << precinct_num[j];
        for(int i = 0; i < 7; i++)
        {
            cout << num_votes[j][i];
        }
    }

    dataFile.close();
    cout << "End of file";
    return 0;
}

bool openFileIn(fstream &file, char *name)
{
    file.open(name, ios::in);
    return !file.fail();
}

【讨论】:

  • @Mike :如果您的唯一目标是“使用指针”,那么最简单的做法是使用 new[] 而不是在堆栈上分配您的数组。 (别忘了delete[]他们。)
【解决方案2】:

是的,您对指针的理解似乎存在缺陷。我不明白你为什么在这里使用指针语法。只需删除指针定义和取消引用,它就会做你想做的事。您正在声明一个指向整数的指针数组。也就是说,数组的每个元素都有一个指向 int 而不是 int 的指针。您的代码似乎希望每个数组元素都保存一个 int。

我建议您只需查找有关 C++ 指针的好教程。

【讨论】:

    【解决方案3】:

    您首先需要为 precinct_num 和 num_votes 分配一些空间。要么使用 malloc/calloc,要么直接将其放在堆栈上。

    【讨论】:

    • 我不相信这是真的。这些只是局部变量数组,因此它们将获得自动分配。
    • @Tod - 不,他已经声明了指针数组。本地分配是针对指针,而不是它们所指向的。
    • @Brian - 我同意,但我认为理查德是说指针数组需要分配,也许他的意思是它们指向的需要分配(更不用说分配)。
    【解决方案4】:

    您应该更改 precinct_num 和 num_votes 变量的声明。现在它们是数组的指针。您遇到的错误是这些指针没有指向任何东西!所以你不希望 pointers 指向数组。你只想要直接的数组。所以把它们改成:

    int precinct_num[PREC_SIZE];
    int num_votes[PREC_SIZE][CANIDATES];
    

    然后,当您读取值时,您只想:

    dataFile >> precinct_num[counter];
    

    和

    dataFile >> num_votes[counter][i];
    

    等等……

    这只是指针方面。我没有查看一般的代码,看看它是否在做正确的事情。

    【讨论】:

    • 其实他有指针数组。你总是有一个指向数组的指针;)
    • 啊,你是对的!对不起。这个C力很弱。 :)
    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 2021-10-31
    • 2012-07-21
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多