Translate C++ code to C# - pointers

Hello!

I am trying to translate some C++ code to C# for Unity. My problem is, as I am not very familiar with pointers, that I don’t know how to translate these without the need to enter the unsafe mode. The snippet is part of a method that finds the intersection point of three planes. Note: The C++ code was written by Ronan Tzur and is public domain.

Sorry if the extract is too bit long and if the question is “noobie” :slight_smile:

Thank you for your time!

for (int i = 0; i < 3; ++i) {
			
			// set up a vector to reference into the matrix
			// from mat(i,i) to mat(m,i), that is, from the
			// i'th column of the i'th row and down all the way
			// through that column
			double *ptrs[MAXROWS];
			int num_ptrs = rows - i;
			for (int q = 0; q < num_ptrs; ++q)
				ptrs[q] = &mat[q + i]*;*
  •  	// all computations below this point are performed*
    
  •  	// only for the first two columns:  i=0 or i=1*
    
  •  	if (i + 1 < 3) {*
    
  •  		// perform householder transformation on the matrix*
    
  •  		// mat(i,i+1) to mat(m,n), that is, on the sub-matrix*
    
  •  		// that begins in the (i+1)'th column of the i'th*
    
  •  		// row and extends to the end of the matrix at (m,n)*
    
  •  		if (tau != 0.0) {*
    
  •  			for (int x = i + 1; x < 3; ++x) {*
    

_ double wx = mat*;_
_
for (y = i + 1; y < rows; ++y)_
_ wx += mat[y][x] * (ptrs[y - i]);_
double tau_wx = tau * wx;
mat[x] -= tau_wx;

* for (y = i + 1; y < rows; ++y)*
mat[y] -= tau_wx * (ptrs[y - i]);
_ }
}*_

* // perform householder transformation on i’th row*
* // (remember at this point, i is either 0 or 1)*

* // set up a vector to reference into the matrix*
* // from mat(i,i+1) to mat(i,n), that is, from the*
* // (i+1)'th column of the i’th row and all the way*
* // through to the end of that row*
_ ptrs[0] = &mat*[i + 1];
if (i == 0) {
ptrs[1] = &mat[i + 2];_

num_ptrs = 2;
_ } else // i == 1*_

* num_ptrs = 1;*

* }*
}

I have no idea what tau is in the context of the code, i assume it’s a double or float by it’s usage, the only thing that is really different here with c++ and c# is the pointers and the dereferencing, other then that it’s pretty much removing the & and *.

Fantastic explanation of pointers on [stackoverflow][1].

for (int i = 0; i < 3; ++i) 
{
	// set up a vector to reference into the matrix
	// from mat(i,i) to mat(m,i), that is, from the
	// i'th column of the i'th row and down all the way
	// through that column
	double ptrs[MAXROWS];
	int num_ptrs = rows - i;
	for (int q = 0; q < num_ptrs; ++q)
			ptrs[q] = mat[q + i]*;*
  •  	// all computations below this point are performed*
    
  • // only for the first two columns: i=0 or i=1*
  • if (i + 1 < 3)*
  • {*
  •  	// perform householder transformation on the matrix*
    
  •  	// mat(i,i+1) to mat(m,n), that is, on the sub-matrix*
    
  •  	// that begins in the (i+1)'th column of the i'th*
    
  •  	// row and extends to the end of the matrix at (m,n)*
    
  •  	if (tau != 0.0d)* 
    
  •  	{*
    
  •  			for (int x = i + 1; x < 3; ++x)* 
    
  •  			{*
    

_ double wx = mat*;_
_
for (y = i + 1; y < rows; ++y)_
_ wx += mat[y][x] * (ptrs[y - i]);_
double tau_wx = tau * wx;
mat -= tau_wx;
_ for (y = i + 1; y < rows; ++y)
mat[y][x] -= tau_wx * (ptrs[y - i]);
}
}*_

* // perform householder transformation on i’th row*
* // (remember at this point, i is either 0 or 1)*

* // set up a vector to reference into the matrix*
* // from mat(i,i+1) to mat(i,n), that is, from the*
* // (i+1)'th column of the i’th row and all the way*
* // through to the end of that row*
_ ptrs[0] = mat*[i + 1];
if (i == 0)
{
ptrs[1] = mat[i + 2];_

num_ptrs = 2;
_ }
else // i == 1*

* {_
num_ptrs = 1;
_ }
}
}
[1]: What does 'dereferencing' a pointer mean in C/C++? - Stack Overflow