简体   繁体   English

错误:在 '.' 之前应有 '='、','、';'、'asm' 或 '__attribute__' 令牌

[英]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.我正在尝试为 C 中的 Game Boy Advance 创建一个 1bpp bitmap 字体,本质上我想创建一个由(ASCII CODE - 32)* 8 索引的连续 ROM 数据区域。 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:编辑:将所有内容移入 main 会给我一个不同的错误:

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.这个我也看不懂

You can only initialise on instantiation.您只能在实例化时进行初始化 You have attempted an assignment outsode of any function - that will not work.您已尝试对任何 function 进行分配- 这将不起作用。

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

You can initialize variables declared at file scope but you can't assign to them, because assignment is regarded as run-time code.您可以初始化在文件 scope 中声明的变量,但不能分配给它们,因为分配被视为运行时代码。 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.此外,这听起来像是不应在运行时更改的内容,因此您应该添加const并确保它在 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};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM