本文介绍了将2D数组传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个简短的问题.我正在尝试采用我编写的一堆代码,然后复制/粘贴10次..并将其转换为函数.我仍然在努力传递2D数组和指针.我有通过以下方式传递 square 2D数组的函数:

  void  createTrans( double  orig [ 3 ], double (* rot)[ 3 ], double (* transform)[ 4 ]){
     for ( int  i =  0 ; i< ; 3; i ++){
         for ( int  j =  0 ; j< ; 3; j ++){
            transform [i] [j] = rot [i] [j];
            如果(j ==  2 ){
                transform [i] [j + 1] = orig [i];
            }
        }
    }
    transform [ 3 ] [ 3 ] =  1 ; transform [ 3 ] [ 0 ] = transform [ 3 ] [ 1 ] = transform [ 3 ] [ 2 ] =  0 ;
} 



现在,这是我要制作成函数的代码:

double LG[100][66];

	ifstream LGFile("mtlength\\LG.txt", ios::in);
	if (LGFile.is_open())
	{
		stringstream iss, isn;
		string line, word;

		i=0; j=0;

		while (getline(LGFile,line))
		{
			isn.clear();
			isn << line;

			while (getline(isn,word,''\t'')) {
				if(j==(66)) {break;}
				LG[i][j]=atof(word.c_str());
				j++;
			}
			j=0; i++;
			isn.clear();
		}
	}
	LGFile.close();



所以我的问题很简单.如何将我的LG矩阵(n x m)传递给函数作为指针,可以通过文本文件填充它.也许我只是假设double (*rot)[3]声明指定了3x3,但没有.

解决方案


Hey guys, I had a quick question. I''m am trying to take a bunch of code I wrote and then copy/pasted 10 times.. and turn it into a function. Passing 2D arrays and pointers is still something I''m struggled with. I have functions that pass square 2D arrays in the following way:

void createTrans(double orig[3], double (*rot)[3], double (*transform)[4]){
    for (int i=0;i<3;i++){
        for (int j=0;j<3;j++){
            transform[i][j] = rot[i][j];
            if (j==2) {
                transform[i][j+1] = orig[i];
            }
        }
    }
    transform[3][3]=1; transform[3][0]=transform[3][1]=transform[3][2]=0;
}



Now here is the code I am trying to make into a function:

double LG[100][66];

	ifstream LGFile("mtlength\\LG.txt", ios::in);
	if (LGFile.is_open())
	{
		stringstream iss, isn;
		string line, word;

		i=0; j=0;

		while (getline(LGFile,line))
		{
			isn.clear();
			isn << line;

			while (getline(isn,word,''\t'')) {
				if(j==(66)) {break;}
				LG[i][j]=atof(word.c_str());
				j++;
			}
			j=0; i++;
			isn.clear();
		}
	}
	LGFile.close();



So my question is fairly simple. How do I pass my LG matrix (of n x m) to a function as a pointer where it can be filled via the text file. Maybe I am just assuming that the double (*rot)[3] declaration specifies a 3x3 but it doesn''t. Any help on this would be much appreciated.

解决方案


这篇关于将2D数组传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:58
查看更多