簡體   English   中英

將char [,]數組轉換為char **

[英]Converting char[,] array to char**

在c#中將字符串轉換為char*很容易

string p = "qwerty";
fixed(char* s = p)

但有沒有人知道如何在c#中將char[,]轉換為char**

下面的代碼顯示了如何將char[,]數組轉換為指針。 它還演示了如何將字符寫入數組並通過指針檢索。 您也可以使用指針編寫並使用數組進行讀取。 它完全相同,因為它引用了相同的數據。

            char[,] twoD = new char[2, 2];

            // Store characters in a two-dimensional array
            twoD[0, 0] = 'a';
            twoD[0, 1] = 'b';
            twoD[1, 0] = 'c';
            twoD[1, 1] = 'd';

            // Convert to pointer
            fixed (char* ptr = twoD)
            {
                // Access characters throught the pointer
                char ch0 = ptr[0]; // gets the character 'a'
                char ch1 = ptr[1]; // gets the character 'b'
                char ch2 = ptr[2]; // gets the character 'c'
                char ch3 = ptr[3]; // gets the character 'd'
            }

暫無
暫無

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

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