2

我正在为 Arduino 编程。我想使用一个数组,但我想更改数组的内容,而代码正在使用我用来初始化数组的相同代码运行:

我可以做这个:

boolean framebuffer[6][5] = {
    {0,0,1,0,0},
    {0,0,0,1,0},
    {0,0,1,0,0},
    {0,1,0,0,0},
    {1,0,0,0,0},
    {1,1,1,1,1}
  };

但我不能这样做:

  framebuffer = {
    {0,0,1,0,0},
    {0,0,0,1,0},
    {0,0,1,0,0},
    {0,1,0,0,0},
    {1,0,0,0,0},
    {1,1,1,1,1}
  };

有没有可能像这样设置数组内容?我不想单独分配每个数组元素,如下所示:

  framebuffer[0][0] = 0;
4

1 回答 1

1

您不能直接这样做,但是您可以预定义所有数组,然后将memcpy它们framebuffer

// Put all your preconstructed items in some array.....
// You'd typically make this a global.

boolean glyphs[2][6][5] = {
    {
        {0,0,1,0,0},
        {0,0,0,1,0},
        {0,0,1,0,0},
        {0,1,0,0,0},
        {1,0,0,0,0},
        {1,1,1,1,1}
    },
    {
        {1,1,1,1,1},
        {1,0,0,1,1},
        {1,0,1,0,1},
        {1,1,0,0,1},
        {1,0,0,0,1},
        {1,1,1,1,1}
    }
};

// Then whereever you want to change the framebuffer in your code:
// copy the second into a framebuffer:
memcpy(framebuffer, glyphs[1], sizeof(boolean)*6*5);
于 2012-12-09T20:25:00.273 回答