C++:访问作为函数参数传递的双指针数组指向的变量值
C++: accessing values of variables pointed by double pointer array passed as argument of function
我正在尝试访问二维双指针数组中指针指向的值,作为函数参数传递
float cfun (float **indatav, int rows, int cols)
{
float* *outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
outdatav[i][j] = *(*(indatav[i])[j]);
}
}
return outdatav;
}
我尝试了几种变体,但其中 none 有效。这个变体在我看来是最正确的,但不起作用,我不明白为什么。任何提示或解释将不胜感激。
我知道 outdatav[i][j] = indatav[i][j];
和 return 类型的 float**
可以工作,但这不是我要找的,因为在这种情况下函数将 return 双指针的内存地址,而我正在寻找这些指针指向的变量的值。
一个可重现的例子是:
#include <iostream>
#include <vector>
using namespace std;
//float cfun (float **indatav, int rows, int cols)
float** cfun (float **indatav, int rows, int cols)
{
float** outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
outdatav[i][j] = *(*(indatav[i])[j]);
}
}
return outdatav;
}
int main(){
// float indatav[2][2] = {{1.,2.};{3;4}} // WRONG!
float indatav[2][2] = {{1.,2.},{3,4}};// RIGHT!
int rows = 2, cols = 2;
// float y[2][2] = cfun(**indatav, rows, cols); // Try compiling this!
float **y = cfun(indatav, rows, cols); // THIS DOES WHAT YOU ARE AKSING!
return 0;
}
其中 y
的值为 {{1.,2.},{3,4}}
。 // 注意变化
当你 正确地 取消引用你的 outdatav
二维数组时,你看起来很舒服,那么是什么让你认为 indatav
二维数组应该被区别对待吗?尝试以下两行更正:
float** cfun (float **indatav, int rows, int cols) // Now return 2D pointer!
{
float* *outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
// outdatav[i][j] = *(*(indatav[i])[j]);
outdatav[i][j] = indatav[i][j]; // Why don't you think this will work?
}
}
return outdatav; // Note change in function declaration!
}
毕竟,outdatav
和 indatav
都有 完全 相同的声明 - 都是 float**
.
在您编辑的问题中,您现在有一个电话:y = cfun(**indatav, rows, cols);
并添加
Where y would be have the values {{1.,2.},{3;4}}
这就是完全我如何解释你想要的。您需要将变量 "y," 声明为:float** y
,第 0 行和第 1 列数据的实际值将由表达式 y[0][1]
给出。但是,您对 cfun
的调用是错误的 并且应该是:y = cfun(indatav, rows, cols);
对于您给出的示例。
经过长时间的讨论,我了解到我没有正确传递函数中的参数,并且弄乱了指针和数据类型。
这是最终答案:
#include <iostream>
#include <vector>
using namespace std;
float** cfun (float **indatav, int rows, int cols)
{
float* *outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
outdatav[i][j] = indatav[i][j];
}
}
return outdatav;
}
int main(){
int rows = 2, cols = 2;
float list[rows][cols]= {{1.,2.},{3.,4.}};
float* *indatav = new float*[rows];
for (int i=0; i < rows; i++){
indatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
indatav[i][j]=list[i][j];
}
}
float **y = cfun(indatav, rows, cols);
for(int i=0; i<rows; i++){
for(int j=0; j<rows; j++){
cout << y[i][j] <<endl;
}
}
return 0;
}
显然,我愿意为主要功能提供任何更优雅的解决方案。
我正在尝试访问二维双指针数组中指针指向的值,作为函数参数传递
float cfun (float **indatav, int rows, int cols)
{
float* *outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
outdatav[i][j] = *(*(indatav[i])[j]);
}
}
return outdatav;
}
我尝试了几种变体,但其中 none 有效。这个变体在我看来是最正确的,但不起作用,我不明白为什么。任何提示或解释将不胜感激。
我知道 outdatav[i][j] = indatav[i][j];
和 return 类型的 float**
可以工作,但这不是我要找的,因为在这种情况下函数将 return 双指针的内存地址,而我正在寻找这些指针指向的变量的值。
一个可重现的例子是:
#include <iostream>
#include <vector>
using namespace std;
//float cfun (float **indatav, int rows, int cols)
float** cfun (float **indatav, int rows, int cols)
{
float** outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
outdatav[i][j] = *(*(indatav[i])[j]);
}
}
return outdatav;
}
int main(){
// float indatav[2][2] = {{1.,2.};{3;4}} // WRONG!
float indatav[2][2] = {{1.,2.},{3,4}};// RIGHT!
int rows = 2, cols = 2;
// float y[2][2] = cfun(**indatav, rows, cols); // Try compiling this!
float **y = cfun(indatav, rows, cols); // THIS DOES WHAT YOU ARE AKSING!
return 0;
}
其中 y
的值为 {{1.,2.},{3,4}}
。 // 注意变化
当你 正确地 取消引用你的 outdatav
二维数组时,你看起来很舒服,那么是什么让你认为 indatav
二维数组应该被区别对待吗?尝试以下两行更正:
float** cfun (float **indatav, int rows, int cols) // Now return 2D pointer!
{
float* *outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
// outdatav[i][j] = *(*(indatav[i])[j]);
outdatav[i][j] = indatav[i][j]; // Why don't you think this will work?
}
}
return outdatav; // Note change in function declaration!
}
毕竟,outdatav
和 indatav
都有 完全 相同的声明 - 都是 float**
.
在您编辑的问题中,您现在有一个电话:y = cfun(**indatav, rows, cols);
并添加
Where y would be have the values {{1.,2.},{3;4}}
这就是完全我如何解释你想要的。您需要将变量 "y," 声明为:float** y
,第 0 行和第 1 列数据的实际值将由表达式 y[0][1]
给出。但是,您对 cfun
的调用是错误的 并且应该是:y = cfun(indatav, rows, cols);
对于您给出的示例。
经过长时间的讨论,我了解到我没有正确传递函数中的参数,并且弄乱了指针和数据类型。 这是最终答案:
#include <iostream>
#include <vector>
using namespace std;
float** cfun (float **indatav, int rows, int cols)
{
float* *outdatav = new float*[rows];
for (int i=0; i < rows; i++){
outdatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
outdatav[i][j] = indatav[i][j];
}
}
return outdatav;
}
int main(){
int rows = 2, cols = 2;
float list[rows][cols]= {{1.,2.},{3.,4.}};
float* *indatav = new float*[rows];
for (int i=0; i < rows; i++){
indatav[i] = new float[cols];
}
for (int i=0; i < rows; i++){
for (int j=0; j < cols; j++){
indatav[i][j]=list[i][j];
}
}
float **y = cfun(indatav, rows, cols);
for(int i=0; i<rows; i++){
for(int j=0; j<rows; j++){
cout << y[i][j] <<endl;
}
}
return 0;
}
显然,我愿意为主要功能提供任何更优雅的解决方案。