4

I've been handed a microcontroller project that is in C. I am an accomplished programmer, but not in this language. I 'think' my problem is related to C syntax (and my lack of understanding) and not the microcontroller compiler, so I will ignore the compiler issues for the sake of brevity.

I am attempting to use a Jagged/Ragged array to store font information for quick recall to be displayed on a LCD. There are other ways to attack this problem, but I have reasons for wanting to stick to 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 row that defines that glyph. Examples:

The glyph {A} would need this data: {65,8,12,1,0,0x18, 0x3C, 0x66, 0xC3, 0xC3, 0xC3, 0xC3, 0xFF, 0xFF}

The glyph {'} would need this data: {39,2,3,4,9,0xC0,0xC0, 0xC0}

The glyph {.} would need this 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}

Does this code seem functional? I seem to be throwing an error that is difficult to trap. The assignments are not working as expected. When the compiler hits the first one, it resets the project.

Thanks in advance for your help.

Cartoondog
  • 105
  • 1
  • 8
  • 3
    what is your question? – Jens Gustedt Oct 15 '14 at 21:05
  • Does this code look correct? I seem to be throwing errors not easy to trap. – Cartoondog Oct 15 '14 at 21:15
  • 2
    More detailed _I seem to be throwing errors_. Where are you seeing them? You have not provided enough code to completely compile anything. – ryyker Oct 15 '14 at 21:16
  • ryyker snarky comments don't help anyone. It helps if you read the entire question and answer it if you can. If you cannot, leave it for someone else. Your comment seems aimed at upping your score rather than providing real help. I am not going to upload my entire project and hope you can find the bit of code that needs help. – Cartoondog Oct 15 '14 at 21:49
  • @Cartoondog - I apologize that it was taken that way. I literally meant that it would be helpful to have more information. I actually thought you asked a good question. No bad intended. – ryyker Oct 15 '14 at 22:16

2 Answers2

5

If you really need a ragged array, then in order to build it using this in-line syntax you need a modern C compiler that supports compound 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 does not support compound 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 };

Your original code is not valid C. If it compiles, it is only due to some compiler extension, which does not do what you think it does.

P.S. Same problem as 'C' Segmentation fault with 2d array

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

Is this 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, since you do not know the length of the individual arrays. A good way to make it better is to have the size of the array as the first element of the array , read it to check bounds and then index to it. Alternately, you could have a structure to define that too.

struct font {
 short size;
 char pattern[MAX_FONT_SIZE];
}
Subbu
  • 61
  • 2