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






Reply to topic  [ 5 posts ] 
Passing array by reference in RobotC 1.05 
Author Message
Rookie

Joined: Sun Jul 22, 2007 6:32 pm
Posts: 16
Location: Ireland
Post Passing array by reference in RobotC 1.05
Hi,

Is it possible to pass an array by reference in RobotC 1.05?

I'm attempting to pass an array of ints into a function to initialise the array. At a later point I will have another function to update the array with sensor readings.

The following code refuses to compile, with an error on the line where I dereference the pToArray argument: **Error**:Array specifier invalid for variable 'pToArray'

Code:
void initialiseArray(int &pToArray, tSensors sensorToRead)
{
   int i;
   for(i=0; i < kNumSamples; i++)
   {
      pToArray[i] = SensorValue[sensorToRead];
 ......
   }
}


I have also tried using
Code:
void initialiseArray(int &pToArray[], tSensors sensorToRead)

and just plain old
Code:
void initialiseArray(int pToArray[], tSensors sensorToRead)

to declare the function. None of these options will compile.

This code would be called as:

Code:
const int kNumSamples = 50;
int sonarSensorReadings[kNumSamples];
...
initialiseArray(sonarSensorReadings, S4);


Is it possible to pass arrays by reference, and if so what is the correct syntax for doing so?

Many thanks,
Mark


Sun Jul 22, 2007 6:50 pm
Profile WWW
Rookie

Joined: Sat Jun 16, 2007 6:24 pm
Posts: 14
Post 
The easiest way I know how to do this is by wrapping your array in a struct and passing that in. Its not the prettiest code, but it does accomplish what you want, and you may have more you want to encapsulate in that structure at which point it will make more sense to use a struct.

Maybe someone has a better idea, but thats my best first try.

Code:
#define BSIZE 100

typedef struct
{
   int size = BSIZE;
   int array[BSIZE];

} BoundArr;


void initArray(BoundArr a)
{
   for(int i = 0; i < a.size; i++)
      a.array[i] = i*2;
}

void printArray(BoundArr a)
{
   for(int i = 0; i < a.size; i++){
      wait1Msec(100);
      nxtDisplayTextLine(1, "a.array[%d] = %d", i, a.array[i]);
   }
}

task main()
{
   BoundArr a;
   initArray(a);
   printArray(a);
}


I hope that helps. This is the second C related question this weekend. It might be smart if RobotC used an extension other than .c (perhaps .rc) to more explicitly set itself apart from C.

-- David Trotz


Sun Jul 22, 2007 7:18 pm
Profile
Rookie

Joined: Sun Jul 22, 2007 6:32 pm
Posts: 16
Location: Ireland
Post 
Thanks David,

That seems to have fooled the compiler for now! It also allows me to encapsulate more of the data related to the array.

Regards,
Mark


Mon Jul 23, 2007 10:19 am
Profile WWW
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 
This is is bug. It is fixed in version 1.07.

Note that you presently cannot declare array indices explicitly on a procedure parameter. But you can declare a 'typedef' for an array type and then have the type of a procedure parameter be a typedef. See the following code snippet.

I corrected a code generation bug with this that I don't think occurs in the "struct' trick mentioned earlier in this thread.

Code:
typedef int tArray[10][10];
tArray yy;

void testFunction(tArray &xx, const int nAdjust)
{
  int i;
  int j;

  for (i = 0; i < 10; ++i)
    for (j = 0; j < 10; ++j)
    {
      xx[i][j] = nAdjust;
    }
  return;
}

task main()
{
  int i;

  for (i = 0; i < 30000; i += 100)
  {
    testFunction(yy, i);
    wait1Msec(500);
  }
  return;
}


Thu Aug 02, 2007 7:08 pm
Profile
Rookie

Joined: Mon Sep 17, 2007 5:31 am
Posts: 28
Location: Indonesia
Post hey....
I try the program but it can't work...
how can it be???
can you teach me the program use "wait until"...
it's difficult for me...
and how you all use about using set sensor mode????
thanks...


Mon Sep 17, 2007 11:41 pm
Profile YIM
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.