4

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?

4 Answers 4

10

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Note that since you're storing pointers to static strings that you won't be able to modify these strings in the code. If you want to modify them, use them as initializers for arrays as in dirkgently's code.
I did actually think of that, @rampion, but given the microcontroller aspect and the tone in the question, I made the command decision to treat it as a constant table. I may have been wrong in which case you should use one of the 3d, non-pointer type solutions.
Thank you Pax, I think this is probably the simplest way for me to do it given my lack of expertise in C. And you're absolutely right, it IS a static table that I do not need to change. So this solution seems to fit. Thanks to everyone else too for taking the time to help me out! Much appreciated :)
7
char DirectionPosition[][ 5 ][ 3 ] = {{"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"},};

C has no inbuilt string class, you have to make do with character arrays. You could alternatively use pointers to pointer to char.

Comments

2

I recommend deciding the maximum length of strings and maximum number of strings per row and telling the compiler, e.g.:

typedef char string[3];
typedef string s5[5];

    s5 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"},};

Now, DirectionPosition[2][1] &c will let you access specific strings in the matrix.

Comments

1

There's a slightly simpler way of doing this without all the extra brackets:

#include <stdio.h>

int main(int argc, char **argv) {

char DirectionPosition[][ 5 ][ 3 ] = {"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"};

    printf("dp[1][1] == %s\n", DirectionPosition[1][1]);
    printf("dp[1][2] == %s\n", DirectionPosition[1][2]);
    printf("dp[1][3] == %s\n", DirectionPosition[1][3]);

    return;
}

1 Comment

Yeesh. I far prefer the extra brackets for readability. The compiler may be able to easily figure out what entry goes where, but I'd like to be able to as well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.