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

标签 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;

最佳答案

你不能直接那样做,但你可以预定义所有数组,然后将它们memcpyframebuffer:

// 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);

关于我可以使用列表重置数组值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13791326/

相关文章:

c++ - 在C/C++中使用双指针有什么好处

c - POSIX 中未提及的错误

c++ - 软 I²C ping 功能

c - ATmega2560使用UART中断来控制全局标志

c - LED 同时闪烁,具有独立且不一致的模式

python - 使用 pyserial 控制 Arduino Uno 板上的特定引脚

c - 获取错误 : format specifies type 'int' but the argument has type 'double'

c++ - vector 排序和更改数据

python - 如何使用 ctypes 将 C 函数中返回的二维数组传递给 python

arduino - 如何使用 Arduino + ESP8266 AT 命令发送 HTTP 请求