簡體   English   中英

您能理解這個C指針代碼嗎?

[英]Can you make sense of this C pointer code?

我有一段以非常混亂的方式使用指針的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];
   V[line1+1]=h[0]*h[4] + h[3]*h[1];

這里發生了什么事? 我該如何在C#中編寫等效的東西?

您實際上並沒有在C#中編寫等效的東西,因為那里沒有指針(通過調用unsafe代碼除外)-要從C#數組中獲取元素,您需要一個數組ref和一個索引,然后您對該數組進行索引。

當然,您可以對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#中幾乎逐字使用的內容。

&H[9*i] == (H + 9*i) ,因此您可以將h[x]使用替換為H[9*i+x] 其余的應該很簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM