View unanswered posts | View active topics It is currently Fri Apr 18, 2014 4:08 am






Reply to topic  [ 5 posts ] 
Problem: Indexing arrays included in array-structures 
Author Message
Rookie

Joined: Wed Dec 12, 2007 3:32 pm
Posts: 8
Post 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


Wed Dec 12, 2007 5:12 pm
Profile
Expert
User avatar

Joined: Fri Nov 09, 2007 4:51 am
Posts: 121
Location: Hungary, Europe
Post 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()


Thu Dec 13, 2007 1:35 pm
Profile
Rookie

Joined: Wed Dec 12, 2007 3:32 pm
Posts: 8
Post 
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).


Fri Dec 14, 2007 11:41 am
Profile
Expert
User avatar

Joined: Fri Nov 09, 2007 4:51 am
Posts: 121
Location: Hungary, Europe
Post 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


Fri Dec 14, 2007 1:27 pm
Profile
Rookie

Joined: Wed Dec 12, 2007 3:32 pm
Posts: 8
Post 
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.


Wed Dec 19, 2007 5:16 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.