简体   繁体   中英

Converting char[,] array to char**

Converting string to char* is easy in c#

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

But does anyone know how to convert char[,] into char** in c#?

The code below shows how to convert a char[,] array to a pointer. 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'
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM