2012-12-09 19 views
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; 

回答

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); 
+0

聽起來不錯,我該怎麼做? – treaki

+0

更新....用代碼 –

+0

非常感謝,我會這樣做 – treaki

相關問題