Menu

How to declare 2-D array ?

Help
Mian Qi
2024-07-19
2024-07-29
  • Mian Qi

    Mian Qi - 2024-07-19

    When I declared as this:

     45 bool keyPad[4][4] = {
     46   {0, 0, 0, 0},
     47   {0, 0, 0, 0},
     48   {0, 0, 0, 0},
     49   {0, 0, 0, 0}
     50 };
     51 
     52 bool ledArray[8][8] = {
     53   {0, 0, 0, 0, 0, 0, 0, 0},
     54   {0, 0, 0, 0, 0, 0, 0, 0},
     55   {0, 0, 0, 0, 0, 0, 0, 0},
     56   {0, 0, 0, 0, 0, 0, 0, 0},
     57   {0, 0, 0, 0, 0, 0, 0, 0},
     58   {0, 0, 0, 0, 0, 0, 0, 0},
     59   {0, 0, 0, 0, 0, 0, 0, 0},
     60   {0, 0, 0, 0, 0, 0, 0, 0}
     61 };
     62 
    

    It was prompted:

    ?ASlink-Warning-Undefined Global 'FF' referenced by module 'keypad_ledarray'
    

    and, when I declared as this:

     45 bool keyPad[4][4] = {
     46   0, 0, 0, 0,
     47   0, 0, 0, 0,
     48   0, 0, 0, 0,
     49   0, 0, 0, 0
     50 };
     51 
     52 bool ledArray[8][8] = {
     53   0, 0, 0, 0, 0, 0, 0, 0,
     54   0, 0, 0, 0, 0, 0, 0, 0,
     55   0, 0, 0, 0, 0, 0, 0, 0,
     56   0, 0, 0, 0, 0, 0, 0, 0,
     57   0, 0, 0, 0, 0, 0, 0, 0,
     58   0, 0, 0, 0, 0, 0, 0, 0,
     59   0, 0, 0, 0, 0, 0, 0, 0,
     60   0, 0, 0, 0, 0, 0, 0, 0
     61 };
     62 
    

    it was prompted:

    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:47: warning 147: excess elements in array initializer after 'keyPad'
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:121: error 69: struct/union/array '': initialization needs curly braces
    keypad+ledarray.c:54: warning 147: excess elements in array initializer after 'ledArray'
    

    So, how to declare a 2-D array in SDCC ?

     

    Last edit: Mian Qi 2024-07-19
    • Benedikt Freisen

      The first variant is the correct approach, because SDCC does not currently support the second variant, even though it is valid C.
      I have no idea what is going on with the ASlink warning. It is possible that you found a bug.
      If the array is a global variable, you could omit the explicit zero-initialization as a workaround, because global variables are implicitly zeroed.

       
      • Philipp Klaus Krause

        Yes, the first one should work. The error message "?ASlink-Warning-Undefined Global 'FF' referenced by module 'keypad_ledarray'" looks like it is about an unrelated issue. Can you provide a compile- and linkable code sample to reproduce that problem?

         
        • Mian Qi

          Mian Qi - 2024-07-19

          Hier:

           
      • Mian Qi

        Mian Qi - 2024-07-19

        If the array is a global variable, you could omit the explicit zero-initialization as a workaround, because global variables are implicitly zeroed.

        This works.

         
        • Janko Stamenović

          I think the FF about which the linker complains must be here:

          void delay(void) {
          
              __asm
              mov R7, #FF
          

          Instead of #FF there should be either #0xFF or #255, as in this assembler # just means something like "the number argument of the assembly instruction follows" and 0x is a prefix for hexadecimal numbers.

           

          Last edit: Janko Stamenović 2024-07-20
          • Philipp Klaus Krause

            Yes.

            #0xff or #0xFF would be the number 0xff. #FF is the value of the symbol FF (i.e a symbol named FF which would get assigned a value by the linker), but since there is no symbol FF, you get an error message at link time.

             
  • Mian Qi

    Mian Qi - 2024-07-24

    Thanks, when I modified "0xFF", neither of the 2 errors is shown. And, I found

     55 bool keyPad[4][4] = {
     56   {0, 0, 0, 0},
     57   {0, 0, 0, 0},
     58   {0, 0, 0, 0},
     59   {0, 0, 0, 0}
     60 };
    

    was OK, and

     45 /*
     46 bool keyPad[4][4] = { 
     47   0, 0, 0, 0,
     48   0, 0, 0, 0,
     49   0, 0, 0, 0,
     50   0, 0, 0, 0
     51 };
     52 */
    

    would trigger "error 69: struct/union/array '': initialization needs curly braces".

    I attached this feature description here, maybe it is useful for someone else.

     

    Last edit: Mian Qi 2024-07-24
  • Konstantin Kim

    Konstantin Kim - 2024-07-24

    The intriguing aspect of the next two lines is that they follow C23 syntax correctly but SDCC only initialize the first element:

    bool keyPad[4]     = {};
    bool keyPad2[4][4] = {{}};
    

    bug?

     
  • Mian Qi

    Mian Qi - 2024-07-29

    I think this are not bug, since SDCC is a free and open source software, it should trade in-between feature and cost, so what's important is knowing what is can and what it can not, and for those it can, also know how it does them.

     

Log in to post a comment.