Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting char[,] array to char**

Tags:

arrays

c#

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#?

like image 597
alokkoolol123 Avatar asked Nov 09 '22 00:11

alokkoolol123


1 Answers

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'
            }
like image 131
George Avatar answered Nov 14 '22 21:11

George