Subject: 
  | 
            Re: Two dimensional arrays in NXC
  | 
             
            Newsgroups: 
  | 
            lugnet.robotics.nxt
  | 
             
            Date: 
  | 
            Mon, 31 Mar 2008 04:10:21 GMT
  | 
             
            Viewed: 
  | 
            35760 times
  | 
              
     |      | 
             |       |  
      In lugnet.robotics.nxt, Trevyn Watson wrote: 
> Hello, 
>  
> I'm trying to use two dimensional arrays in NXC, like this: 
>  
> int onedarray[4]; 
> int twodarray[4][4]; 
>  
> task main() 
> { 
>       onedarray[2] = 1; 
>       twodarray[2][2] = 1; 
> } 
>  
> I read that the stock firmware doesn't support them, so I got John 
> Hansen's enhanced firmware and put it on my NXT. I downloaded the latest 
> version of BricxCC, and found and checked the "Enhanced firmware" 
> checkbox in the preferences. The code still won't compile. I get 
>  
> line 7: Error: '=' expected 
> line 7: Error: Identifier expected 
> line 7: Error: Undefined Identifier 2 
> line 7: Error: Datatypes are not compatible 
> line 7: Error: ';' expected 
>  
> Am I doing something wrong? 
> How can I use multidimensional arrays? 
 
 
The stock firmware supports multi-dimensional arrays but there is a bug in the 
replace opcode which makes it impossible to replace an element in an array with 
more than one dimension. 
 
In NXC, due to the way the opcodes work in the standard firmware, you manipulate 
elements in multi-dimensional arrays using a 3 step process.  Here's code that 
will work for what you are trying to do: 
 
 
int onedarray[4], tmp[]; 
int twodarray[4][4]; 
 
task main() 
{ 
  onedarray[2] = 1; 
//  twodarray[2][2] = 1; 
  tmp = twodarray[2]; 
  tmp[2] = 1; 
  twodarray[2] = tmp; 
} 
 
I am investigating possibly extending the NXC compiler so that it can handle the 
syntax you were trying to use.  I will have to just create thread-safe temporary 
variables of the required type and dimension and generate the three statements 
above whenever the compiler encounters the twodarray[2][2] = 1 syntax. 
 
Hope this help, 
 
John Hansen 
 |  
       |  
           
   
        Message has 1 Reply:        |    | Re: Two dimensional arrays in NXC
  |  
  |  (...) Well, don't add the functionality on my account - I've redone my program a bit and now it doesn't need multi-dimensional arrays, and I think it's better this way anyways. Thanks for your help. Trevyn    (18 years ago, 2-Apr-08, to lugnet.robotics.nxt)   
   |         
        Message is in Reply To:
            |    | Two dimensional arrays in NXC
  |  
  |  Hello, I'm trying to use two dimensional arrays in NXC, like this: int onedarray[4]; int twodarray[4][4]; task main() { onedarray[2] = 1; twodarray[2][2] = 1; } I read that the stock firmware doesn't support them, so I got John Hansen's enhanced (...)   (18 years ago, 29-Mar-08, to lugnet.robotics.nxt)   
   |         
      9 Messages in This Thread:           
         
       
 
      - Entire Thread on One Page:
      
        
- Nested: 
        All | Brief | Compact | Dots
        
 Linear: 
        All | Brief | Compact
          This Message and its Replies on One Page:
      
        - Nested: 
        All | Brief | Compact | Dots
        
 Linear: 
        All | Brief | Compact
           
         | 
        
  | 
      
 
   | 
           |