puppydrum64
puppydrum64

Reputation: 1688

error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token

I'm trying to create a 1bpp bitmap font for Game Boy Advance in C, essentially I want to create a consecutive region of ROM data that's indexed by (ASCII CODE - 32)*8. So far, I'm getting this error message that I don't understand.

The error:

C:/Users/puppy/Documents/ARMDevTools/SrcGBA/PaintBoyAdvance/source/paintboyadvance.c:35:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
   35 | bitmapfont[0].pixelsPerLetter[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
      |              ^
C:/Users/puppy/Documents/ARMDevTools/SrcGBA/PaintBoyAdvance/source/paintboyadvance.c:36:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
   36 | bitmapfont[1].pixelsPerLetter[] = {0x10,0x18,0x18,0x18,0x18,0x00,0x18,0x00};
      |              ^
================ READY ================

The source code that caused the error (minimal reproducible example):

struct letter{
    char PixelsPerLetter[8];
};

struct letter bitmapfont[96];
bitmapfont[0].PixelsPerLetter[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
bitmapfont[1].PixelsPerLetter[] = {0x10,0x18,0x18,0x18,0x18,0x00,0x18,0x00};


int main(void)
{
    
    return 0;
}

As far as I can tell I'm not missing any semicolons at the end of the lines so I don't see why I would even need the listed characters. This is how you define each struct within the array, no? With a dot like that? That's what all the examples I've seen show and yet I get this error.

EDIT: Moving everything into main gives me a different error:

C:/Users/puppy/Documents/ARMDevTools/SrcGBA/PaintBoyAdvance/source/paintboyadvance.c:12:39: error: expected expression before ']' token
   12 |         bitmapfont[0].PixelsPerLetter[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
      |                                       ^
C:/Users/puppy/Documents/ARMDevTools/SrcGBA/PaintBoyAdvance/source/paintboyadvance.c:13:39: error: expected expression before ']' token
   13 |         bitmapfont[1].PixelsPerLetter[] = {0x10,0x18,0x18,0x18,0x18,0x00,0x18,0x00};

I don't understand this one either.

Upvotes: 1

Views: 275

Answers (2)

Lundin
Lundin

Reputation: 213711

You can initialize variables declared at file scope but you can't assign to them, because assignment is regarded as run-time code. All code executed at run-time must be located inside functions.

As mentioned you can fix this by rewriting the code to use initialization:

struct letter bitmapfont[96] = 
{
  [0] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  [1] = {0x10,0x18,0x18,0x18,0x18,0x00,0x18,0x00},
  ...
};

Also this sounds like something that shouldn't be changed in run-time, so you should add const and ensure it gets allocated in flash.

In case you do need to change it at run-time for whatever reason, it is possible by using compound literals:

{
  // inside a function

  bitmapfont[0] = (struct letter){0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
}

Upvotes: 1

Clifford
Clifford

Reputation: 93476

You can only initialise on instantiation. You have attempted an assignment outside of any function - that will not work.

struct letter bitmapfont[96]= { {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
                                {0x10,0x18,0x18,0x18,0x18,0x00,0x18,0x00} } ;

Upvotes: 2

Related Questions