简体   繁体   中英

How to set 1d array to 2d array element in C

I need something like this:

char font[128][8] = {{0}};

font[0][] = {0, 0b00000000, 0b11111100, 0b00010010, 0b00010010, 0b11111100, 0b00000000, 0};
font[1][] = {...}

But in c99 I get "expected expression before '{' token". Please help.

You can only use an initialiser list ( {...} ) when declaring the array, that's why you're getting an error. You can't assign a value to an array, which is what font[0] is (a char[] ).

You have 3 options:


  •  char font[128][8] = { {0, 0b00000000, 0b11111100, 0b00010010, 0b00010010, 0b11111100, 0b00000000, 0}; {...} } 
  • Assign each value to an element in the array individually: font[0][0] = x , ..., font[127][7] = y (ie. using a loop).

  • memcpy blocks at a time from like a uint64_t ( sizeof(font[0]) = 8 ) or wherever else you can neatly/efficiently store the data.

It's probably also worth noting that binary constants are a C extension, and that char is signed and if you're working with unsigned data you should probably explicitly use unsigned char .

char font[128][8] = {
    {0, 0b00000000, 0b11111100, 0b00010010, 0b00010010, 0b11111100, 0b00000000, 0},//font[0]
    /*{...}*///font[1]
};

Try it out :

    char font[128][8] = {{0}};  
    char a[8] = {0, 0b00000000, 0b11111100, 0b00010010, 0b00010010, 0b11111100, 0b00000000, 0};
    //Take array a to store values 

    for(int i = 0;i<8;i++)
    font[0][i] =  a[i];
    //Assign value of a to font

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM