I'm trying to convert some code from C# to C so that it can be burnt onto a microcontroller.
Could someone please tell me how I would convert a two dimensional string array in C# into something in C?
My C# code looks like this:
string[,] DirectionPosition = {{"00", "10", "", "01", ""},
{"01", "11", "", "02", "00"},
{"02", "12", "", "03", "01"},
{"03", "13", "", "04", "02"},
{"04", "14", "", "", "03"},
{"10", "20", "00", "11", ""},
{"11", "21", "01", "12", "10"},
{"12", "22", "02", "13", "11"},
.
.
.
.
{"44", "", "34", "", "43"},};
And moreover, how would I access the elements? In C# if I wanted the second element in the third row it would simply be DirectionPosition[2,1] but what is the equivalent of that when there is no string in C much less 2-D string arrays?
The easiest way to do it is with char pointers as follows:
char *DirectionPosition[9][5] = {
{"00", "10", "", "01", "" },
{"01", "11", "", "02", "00"},
{"02", "12", "", "03", "01"},
{"03", "13", "", "04", "02"},
{"04", "14", "", "", "03"},
{"10", "20", "00", "11", "" },
{"11", "21", "01", "12", "10"},
{"12", "22", "02", "13", "11"},
{"44", "", "34", "", "43"}
};
The element "10" in the first line is referenced as DirectionPosition[0][1]
(zero-based, first index is line, second is column).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With