C# 你能理解这个C指针代码吗?
我有一段C代码,它以一种非常混乱的方式使用指针C# 你能理解这个C指针代码吗?,c#,c,arrays,visual-c++,pointers,C#,C,Arrays,Visual C++,Pointers,我有一段C代码,它以一种非常混乱的方式使用指针 // We first point to a specific location within an array.. double* h = &H[9*i]; int line1 = 2*n*i; int line2 = line1+6; // ..and then access elements using that pointer, somehow.. V[line1+0]=h[0]*h[1];
// We first point to a specific location within an array..
double* h = &H[9*i];
int line1 = 2*n*i;
int line2 = line1+6;
// ..and then access elements using that pointer, somehow..
V[line1+0]=h[0]*h[1];
V[line1+1]=h[0]*h[4] + h[3]*h[1];
这里发生了什么事?如何用C#?
&H[9*I]==(H+9*I)
,这样您就可以用H[9*I+x]
替换H[9*I+x]
。其余部分应该很简单。&H[9*i]==(H+9*i)
,因此可以用H[9*i+x]
替换H[9*i+x]
。剩下的应该很简单。你没有真正用C编写等价的东西,因为你没有指针(除了调用safe
code),要从C数组中获取元素,你需要一个数组引用和一个索引,然后索引到数组中
当然,对于C数组也可以这样做。我们将C指针算法转换为C数组索引:
int h_index = 9 * i;
int line1 = 2 * n * i;
int line2 = line1 + 6;
V[line1 + 0] = H[h_index] * H[h_index + 1];
V[line1 + 1] = H[h_index] * H[h_index + 4] + H[h_index + 3] * H[h_index + 1];
然后,我们有一些东西可以在C#中几乎一字不差地使用。在C#中并没有编写等价的东西,因为这里没有指针(除了调用
不安全的代码),要从C#数组中获取元素,需要数组引用和索引,然后索引到数组中
当然,对于C数组也可以这样做。我们将C指针算法转换为C数组索引:
int h_index = 9 * i;
int line1 = 2 * n * i;
int line2 = line1 + 6;
V[line1 + 0] = H[h_index] * H[h_index + 1];
V[line1 + 1] = H[h_index] * H[h_index + 4] + H[h_index + 3] * H[h_index + 1];
然后我们有一些可以在C#中一字不差地使用的东西