C ++访问2D数组指针处的值 [英] C++ Accessing Values at pointer of 2D Array

查看:140
本文介绍了C ++访问2D数组指针处的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个动态的二维数组int,它将代表一个标准矩阵。

I need a dynamic 2D array of ints, it will represent a standard matrix. The size and elements are read in from a file at runtime.

从其他堆栈文件中获取方向我设置了我的数组如下:

Taking direction from other stack posts I've setup my array as follows;

void buildArray(ifstream &file, int** 2dArray);
void buildQueue(Queue<int> &Q, int** 2dArray);

int main()
{
    int** 2dArray;
    Queue<int> Q;
    //...
    // open file
    //...
    buildMatrix(file, 2dArray)
    buildQueue(Q, 2dArray)
}

void buildArray(ifstream &file, int** 2dArray)
{
    int size, element;
    while (file.good()) {
        file >> size;

        2dArray = new int*[size];
        for (int i = 0; i < size; i++)
            2dArray[i] = new int[size];

        // now I should be able to use 2dArray[r][c]

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                file >> element;
                2dArray[i][j] = element;
            }
        } 
}

ints存储在每个位置[r] [c]并构建一个队列。我想我的问题是解引用指针...但我不确定。

Then I need to read the ints stored at each position [r][c] and build a queue. I think my problem is dereferencing the pointers... but I'm not sure.

void buildQueue(Queue<int> &Q, int** 2dArray)
{
    int row, column, element;
    // size is passed in as well, size is our rows or columns size here
    for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                row = i;
                column = j;
                element = 2dArray[i][j];   // getting seg fault here!

                Q.push_back(row, column, element);
            }
     }
 }

我可以使用向量。

推荐答案

什么是表?

table = new int*[size];

不应该是

2dArray = new int*[size];

也从函数

更改

void buildArray(ifstream &file, int** 2dArray)

int ** buildArray(ifstream &file)

因为否则buildArray会获取变量的副本。并且此副本现在保留指向所分配的存储器的指针。

because otherwise buildArray gets a copy of the variable. And this copy now holds the pointer to the allocated memory. The pointer in the calling function never gets updated.

在原始代码中,在buildArray调用之前和之后添加一个printf 2dArray。

In your original code add a printf 2dArray before and after the buildArray call.

printf("%p, 2dArray);
buildMatrix(file, 2dArray);
printf("%p, 2dArray);

你会看到2dArray不会改变。所以你基本上是传递一个未初始化的
指针到buildQueue。

You will see that 2dArray doesn't change. So you are essentially passing an uninitialized pointer to buildQueue.

buildArray和调用应该改成这样

buildArray and call should be changed like this

void buildArray(ifstream &file, int** arr)
{
    int size, element;
    while (file.good()) {
        file >> size;

        arr = new int*[size];
        for (int i = 0; i < size; i++)
            arr[i] = new int[size];


        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                file >> element;
                arr[i][j] = element;
            }
        }
    }

    return arr;
}

int main()
{
    int** 2dArray;
    Queue<int> Q;

    //...
    // open file
    //...
    2dArray = buildMatrix(file);
    buildQueue(Q, 2dArray);
}

这篇关于C ++访问2D数组指针处的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆