简体   繁体   English

将char [,]数组转换为char **

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

Converting string to char* is easy in c# 在c#中将字符串转换为char*很容易

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

But does anyone know how to convert char[,] into char** in c#? 但有没有人知道如何在c#中将char[,]转换为char**

The code below shows how to convert a char[,] array to a pointer. 下面的代码显示了如何将char[,]数组转换为指针。 It also demonstrates how chars can be written into the array and retrieved through the pointer. 它还演示了如何将字符写入数组并通过指针检索。 You could also write using the pointer and read using the array. 您也可以使用指针编写并使用数组进行读取。 It's all the same, as it references the same data. 它完全相同,因为它引用了相同的数据。

            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