我可以使用列表重置数组值吗?

时间:2012-12-09 20:12:18

标签: c arduino

我正在为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;

1 个答案:

答案 0 :(得分: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);