我如何遍历多个数组

时间:2019-03-22 16:09:45

标签: c arrays

在函数调用中如何遍历这5个数组?现在,我只是手动遍历每个对象以形成一个特定字符

    const unsigned char pattern1bar[8]={0x10,0x10,0x10,0x10,0x10,0x10,0x10};
    const unsigned char pattern2bar[8]={0x18,0x18,0x18,0x18,0x18,0x18,0x18};
    const unsigned char pattern3bar[8]={0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c};
    const unsigned char pattern4bar[8]={0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e};
    const unsigned char pattern5bar[8]={0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f};



    void LCD_build(int custom, int cursor, const unsigned char *x,const unsigned char x2, int cgdisplay){
       writeLCD(custom,0,0,1); // 1) set custom gram address location
        unsigned char i;
        for( i= 0; i <x2; i++)
            writeLCD(x[i],1,0,1);
       writeLCD(cursor,0,1,1);  // 3) set cursor to location you want to write to
       writeLCD(cgdisplay,1,1,1); // 4) display cgram(0x00) custom character  at current cursor location 
       DELAY_MS(800);

};

//function call
        LCD_build(0x40,0x80,pattern1bar,8,0x00);
        LCD_build(0x40,0x80,pattern2bar,8,0x00);
        LCD_build(0x40,0x80,pattern3bar,8,0x00);
        LCD_build(0x40,0x80,pattern4bar,8,0x00);
        LCD_build(0x40,0x80,pattern5bar,8,0x00);

2 个答案:

答案 0 :(得分:0)

下面是将3个整数数组放入一个二维数组的简单解决方案:

#include <stdio.h>
#define LENGTH 5

const unsigned short a1[] = {1, 4, 2, 4, 1};
const unsigned short a2[] = {3, 4, 2, 1, 2};
const unsigned short a3[] = {4, 12, 1, 1, 2};

void main()
{
    const unsigned short *ar[] = {a1, a2, a3};
    unsigned i, j;

    for (i = 0; i < LENGTH; i++)
    {
        for (j = 0; j < LENGTH; j++)
        {
            printf("%d ", ar[i][j]);
        }

        printf("\n");
    }
}

答案 1 :(得分:0)

创建一个数组数组:

const unsigned char pattern_list[][8] = {
    {0x10,0x10,0x10,0x10,0x10,0x10,0x10},
    {0x18,0x18,0x18,0x18,0x18,0x18,0x18},
    {0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c},
    {0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e},
    {0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f}
};

然后使用

for (int index = 0; index < ARRAY_SIZE(pattern_list); index++) {
    LCD_build(0x40,0x80,pattern_list[index],8,0x00);
}

ARRAY_SIZE在哪里

#define ARRAY_SIZE(array) \
    (sizeof(array) / sizeof(array[0]))