ROBOTC.net forums
http://www.robotc.net/forums/

Problem: Indexing arrays included in array-structures
http://www.robotc.net/forums/viewtopic.php?f=1&t=317
Page 1 of 1

Author:  CHW [ Wed Dec 12, 2007 5:12 pm ]
Post subject:  Problem: Indexing arrays included in array-structures

Hallo,

I have implemented this code, but the NXT module always crashed:

Code:
typedef struct{
               ...
               bool array[n];
               ...
               } structure;

structure struct_array[m];

void main()
{
    ...
    for(int i=0; i<=m; i++)
     {
          for(int j=0; j<=n; j++)
          {
             struct_array[i].array[j]=false;
          }
     }
...
}


After some tests, I found the Problem: I can't index
"struct_array[n].array[m]". Now I also got errors while compiling.
With lower indexes ( n-2, m-2, ...) it has worked.

Does someone have an idea how I could solve this problem?

Thanks

Author:  elemes [ Thu Dec 13, 2007 1:35 pm ]
Post subject:  I can't see the problem

Hello,

I have just created a short test program in RobotC that compiled.
Code:
typedef struct{
               short dummy1;
               bool array[ 10 ];
               long dummy2;
               } structure;

structure struct_array[ 10 ];

task main()
{
   int i;
   int j;

  for( int i = 0; i < 10; i++ ) {
          for(int j = 0; j <10 ; j++) {
              struct_array[ i ].dummy1 = i;
             struct_array[ i ].array[ j ] = false;
             struct_array[ i ].dummy2 = j;
          }
     }
}
What troubles me a bit is the complier warning message Unreferenced variable i and j however, there is nothing wrong with the generated code (i haven't checked it on the brick yet):
Code:
  for( int i = 0; i < 10; i++ ) {
0004: XX5600                   i = 0                             
0007: XX09                     BranchNear(L0011)                   // Jump to test condition
0009: XX572B56          L0009: G87(short) = i                     
000D: XX560001                 i += 1                             
0011: XX020A2B565500    L0011: TestAndBranchIntFar(10 <= i, L006B)
          for(int j = 0; j <10 ; j++) {
0018: XX5700                   j = 0                             
001B: XX09                     BranchNear(L0025)                   // Jump to test condition
001D: XX582B57          L001D: G88(short) = j                     
0021: XX570001                 j += 1                             
0025: XX020A2B573F00    L0025: TestAndBranchIntFar(10 <= j, L0069)
              struct_array[ i ].dummy1 = i;
002C: XX2B58002B5600020800     G88(short) = i * 8                 
0036: XX2F58042B5600           G04[G88].dummy1 = i               
             struct_array[ i ].array[ j ] = false;
003D: XX2B58002B5600020800     G88(short) = i * 8                 
0047: XX2B58002F5805           G88 = addrOf G05[G88]               // Convert to RAM address
004E: XX582B57                 G88 += offset(G87)                 
0052: XX3B5800                 *G04[G88].G87 = 0                 
             struct_array[ i ].dummy2 = j;
0056: XX2B58002B5600020800     G88(short) = i * 8                 
0060: XX2F580A2B5700           G04[G88].dummy2 = j               
0067: XXCB                     BranchNear(L001D)                   // Jump to end Expression
0069: XXE1              L0069: BranchNear(L0009)                   // Jump to end Expression
          }
     }
}
006B: XX                L006B: Return()                            // main()

Author:  CHW [ Fri Dec 14, 2007 11:41 am ]
Post subject: 

On my brick it wasn't working, but
Code:
for(int i=0; i<m; i++)
     {
              struct_array[i].array[n]=false;
     }


and

Code:
 for(int j=0; j<n; j++)
          {
             struct_array[m].array[j]=false;
          }

worked.

I saw that struct_array[i].array[j] in one line didn't work on my brick.

So I have changed the code:

Code:
for(int i=0; i<m; i++)
     {

             struct_array[i].array[0]=false;
             struct_array[i].array[1]=false;
             ...
             struct_array[i].array[n-1]=false;

     }


and my programm was now running on the brick.

I only got the compile error, because RobotC is different from normal C.
In C I used to write structure array[n] and the array goes from array[0] to array[n] (n+1 indexes).
I didn't know that in RobotC you get for structure array[n] only an array from array[0] to array[n-1] (n indexes).

Author:  elemes [ Fri Dec 14, 2007 1:27 pm ]
Post subject:  More than strange

Hello,

Didn't you try to copy the above source code to your robotc and compile?

If the very same code does not compile on your PC while it does on mine
1. we use different RobotC versions
2. there can be some configuration discrepancy between our installation
3. there is come random behavior in the compiler I haven't seen yet.

Laszlo

Author:  CHW [ Wed Dec 19, 2007 5:16 am ]
Post subject: 

Hello,

I have tryed your code and I can compile it without any problems or errors.
But when I try to run it on the brick, it always, like my own code crashes.

I use RobotC version 1.10.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/