Ragged array in C

I am provided with a microcontroller project which is in C. I am an experienced programmer, but not in this language. I think “my problem is with the C syntax (and my lack of understanding), not the microcontroller compiler, so I will ignore the compiler issues for brevity.

I am trying to use a Jagged / Ragged array to store information about fonts for quick recall that will be displayed on the LCD. There are other ways to attack this problem, but I have reasons for wanting to stick with this concept.

Let's assume for this example that there are only three glyphs (characters) in my font. What is unknown is the length of each line that defines this character. Examples:

The glyphs {A} require the following data: {65,8,12,1,0,0x18, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF}

The glyphs {'} require the following data: {39,2,3,4,9,0xC0,0xC0, 0xC0}

The glyphs {.} Require the following data: {46,2,2,4,0,0xC0,0xC0}

With that in mind, here is my code.

//This is where I attempt to make my Jagged array.
 char X,Y, LeftOffSet, BaseOffSet;
 char * font[3]={{46,2,2,4,0,0xC0,0xC0},
                {39,2,3,4,9,0xC0,0xC0, 0xC0},
                {65,8,12,1,0,0x18, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF}};

//None of these assignments work properly.
 LeftOffSet=font[0][0];  // expected assignment = {46}
 LeftOffSet=font[1][4];  // expected assignment = {9}
 LeftOffSet=font[2][1];  // expected assignment = {8}

      

Is this code functional? I seem to be throwing an error that is hard to catch. The appointments are not working as expected. When the compiler hits the first one, it dumps the project.

Thanks in advance for your help.

+3


source to share


2 answers


If you really want a dangling array, then to create it using this built-in syntax, you need a modern C compiler that supports complex literals

char *font[] = { 
  (char []) {46,2,2,4,0,0xC0,0xC0},
  (char []) {39,2,3,4,9,0xC0,0xC0, 0xC0},
  (char []) {65,8,12,1,0,0x18, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF}
};

      

If your compiler doesn't support complex literals, you have no choice but to define the inner arrays as separate named objects



char row0[] = {46,2,2,4,0,0xC0,0xC0},
     row1[] = {39,2,3,4,9,0xC0,0xC0, 0xC0},
     row2[] = {65,8,12,1,0,0x18, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF},
     *font[] = { row0, row1, row2 };

      

The original code is invalid. C. If it compiles, it is only due to some compiler extension that does not do what you think.

PS Same problem as 'C' Segmentation fault with 2d array

+6


source


This is what you are looking for:

    char X,Y, LeftOffSet, BaseOffSet;
    char font1[7] ={46,2,2,4,0,0xC0,0xC0}; 
    char font2[8] ={39,2,3,4,9,0xC0,0xC0, 0xC0}; 
    char font3[15] ={65,8,12,1,0,0x18, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF}; 

    char *fonts[] = {font1, font2, font3};

    main (int argc, char *argv[]) {
    //None of these assignments work properly.
     LeftOffSet = fonts[0][0];
    }

      



Note that this is dangerous as you don't know the length of the individual arrays. A good way to do it better is to have the size of the array as the first element of the array, read it to check the bounds, and then index it. Alternatively, you may have a structure that can define this as well.

struct font {
 short size;
 char pattern[MAX_FONT_SIZE];
}

      

0


source







All Articles