在 C# 中从锯齿状数组转换为双指针 [英] Converting from a jagged array to double pointer in C#
问题描述
这里有一个简单的问题:有没有办法将锯齿状数组转换为双指针?
Simple question here: is there any way to convert from a jagged array to a double pointer?
例如将 double[][]
转换为 double**
不幸的是,这不能仅通过强制转换来完成(就像在普通的旧 C 中一样).使用 fixed
语句似乎也不能解决问题.在 C# 中是否有任何(最好是尽可能有效的)方法来实现这一点?我怀疑解决方案可能根本不是很明显,尽管我仍然希望有一个简单的解决方案.
This can't be done just by casting unfortunately (as it can in plain old C), unfortunately. Using a fixed
statement doesn't seem to resolve the problem either. Is there any (preferably as efficient as possible) way to accomplish this in C#? I suspect the solution may not be very obvious at all, though I'm hoping for a straightforward one nonetheless.
推荐答案
一个 double[][] 是一个 double[] 的数组,而不是 double* 的数组,所以要得到一个 double** ,我们首先需要一个 double*[]
A double[][] is an array of double[], not of double* , so to get a double** , we first need a double*[]
double[][] array = //whatever
//initialize as necessary
fixed (double* junk = &array[0][0]){
double*[] arrayofptr = new double*[array.Length];
for (int i = 0; i < array.Length; i++)
fixed (double* ptr = &array[i][0])
{
arrayofptr[i] = ptr;
}
fixed (double** ptrptr = &arrayofptr[0])
{
//whatever
}
}
我不禁想知道这是为了什么,是否有比需要双指针更好的解决方案.
I can't help but wonder what this is for and if there is a better solution than requiring a double-pointer.
这篇关于在 C# 中从锯齿状数组转换为双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!