【问题标题】:Failing to change values of a pointer matrix无法更改指针矩阵的值
【发布时间】:2017-03-21 05:50:29
【问题描述】:

我在使用指针动态更改矩阵值时遇到问题。

我有这些全局声明:

int row, col = 0;
float** matrixP;
float** matrixT;
float** matrixP_;

然后我有一个函数可以从用户那里获取输入以填充 我想要的任何矩阵

void TakeInput(float** matrix, float row, float col) {

// Initializing the number of rows for the matrix
matrix = new float*[row];

// Initializing the number of columns in a row for the matrix
for (int index = 0; index < row; ++index)
    matrix[index] = new float[col];

// Populate the matrix with data
for (int rowIndex = 0; rowIndex < row; rowIndex++) {
    for (int colIndex = 0; colIndex < col; colIndex++) {
        cout << "Enter the" << rowIndex + 1 << "*" << colIndex + 1 << "entry";
        cin >> matrix[rowIndex][colIndex];
    }
}

// Showing the matrix data
for (int rowIndex = 0; rowIndex < row; rowIndex++) {
    for (int colIndex = 0; colIndex < col; colIndex++) {
        cout << matrix[rowIndex][colIndex] << "\t";
    }
    cout << endl;
}
}

然后我有主要功能我正在接受输入并只是试图显示矩阵P

int main() {
// Take the first point input
cout << "Enter the row and column for your points matrix" << endl;
cout << "Enter the number of rows : "; cin >> row;
cout << "Enter the number of columns : "; cin >> col;

TakeInput(matrixP, row, col);
cout << "=============================================================" << endl;
// =============================================================

for (int rowIndex = 0; rowIndex < row; rowIndex++) {
    for (int colIndex = 0; colIndex < col; colIndex++) {
        cout << matrixP[rowIndex][colIndex] << "\t";
    }
    cout << endl;
}
return 0;
}

现在我在这部分遇到了问题:

for (int rowIndex = 0; rowIndex < row; rowIndex++) {
    for (int colIndex = 0; colIndex < col; colIndex++) {
        cout << matrixP[rowIndex][colIndex] << "\t";
    }
    cout << endl;
}

我得到了:

// matrixP is throwing access violation error.

请在这里需要帮助来指出我在这里做错了什么。 提前致谢!

【问题讨论】:

  • 简化,简化,简化。如果你不能,make a wrapper class to hide the nastiness.
  • 伙计们来吧,我不想在这里优化我的代码,我想找出我的混乱代码做错了什么。我可以使用类,我可以使用许多其他方法来简化我的事情,但我在这里从逻辑上找出我做错了什么。
  • 此时我什至没有考虑优化。那是奖金。我的观点是创建、管理和传递包含float **matrix 比裸float ** 要容易得多。例如,TakeInput 变为 matrix TakeInput(int row, int col)。所有的指针疯狂都打包在matrix 中,您只需要正确处理一次。

标签: c++ pointers matrix


【解决方案1】:

您收到该错误的原因是您将矩阵传递为 “按值传递”而不是通过“引用”传递,所以用这个替换你的代码

void TakeInput(float** &matrix, int row, int col)

row 和 col 也应该是整数。

【讨论】:

  • 轻微的唠叨:int 允许调用者提供负数,而负数组维度......不好。像size_t 这样的无符号整数会稍微安全一些。
【解决方案2】:

很简单,您将 matrixP 按值传递给 TakeInput,即您在 TakeInput 中编辑它的副本。改为这样做。

void TakeInput(float*** matrix, float row, float col) {
//use (*matrix) instead
//...
}


TakeInput(&matrixP, row, col);

http://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/

在这方面,指针与通常的整数没有什么不同。

编辑:

void TakeInput(float** &matrix, float row, float col) {

// Initializing the number of rows for the matrix
matrix = new float*[row];

// Initializing the number of columns in a row for the matrix
for (int index = 0; index < row; ++index)
    matrix[index] = new float[col];

// Populate the matrix with data
for (int rowIndex = 0; rowIndex < row; rowIndex++) {
    for (int colIndex = 0; colIndex < col; colIndex++) {
        cout << "Enter the" << rowIndex + 1 << "*" << colIndex + 1 <<         "entry";
        cin >> matrix[rowIndex][colIndex];
    }
}

【讨论】:

  • 这里的 (float***) 是什么意思?如果我这样做,我会在上面使用 for 循环访问 matrixP 时遇到错误。你能在这里解释一下吗?如果我不这样做,我会收到错误:“float ***”类型的参数与“float **”类型的参数不兼容
  • float*** 是指向float** 的指针。如果需要更改函数内的值,可以通过传递指针来通过引用传递值。但是如果你需要通过引用传递一个指针呢?好吧,一种方法是将指针传递给指针,以便您可以更改指针的值。更好的方法是参考float**:float **&amp; matrix。除其他外,这使得传递 NULL 指针变得更加困难
  • 指针指向指针,就这么简单。如果它是 C++ 而不是 C,那么您可以将其更改为传递引用:void TakeInput(float** &amp;matrix, float row, float col) {。然后你的其余代码不变。
  • 好的,我明白了,现在我将如何初始化它,因为我试图在上面进行初始化:matrix = new float*[row];
  • 几乎一样,只是将函数声明从按值传递更改为通过添加与号(&)的引用传递。请参阅我的更新答案。
猜你喜欢
  • 2017-08-24
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多