I’m trying to create some header functions in ROBOTC for things like finding a max of a given set of variables. I’m trying to keep the code as flexible as possible, and found variable argument lists. I tried porting over the code (native to C), and the compiler wants me to make the function an intrinsic type. After doing so, it wants me to make an asm defintion (not entirely sure what this means or is even possible)? The declaration of the function I’m trying to compile is:
intrinsic float FindMaxFloat(int n, float a, ...) {}
I’ve been trying to port over this
(http://www.cplusplus.com/reference/cstdarg/va_arg/).
Does anybody have any tips for either a) making a variable argument list work or b) creating a workaround?
ROBOTC does not support variable argument lists as far as I know. The best I could come up with in situations like this was to use initialized optional variables a bit like this.
void
DriveSystemAttachControls( short c1, short c2 = (-1), short c3 = (-1), short c4 = (-1), short c5 = (-1), short c6 = (-1))
{
// do something with c1 through c6
}
This could be called as
DriveSystemAttachControls(1);
or
DriveSystemAttachControls(1, 2);
etc.
I used this technique quite a bit in my ROBOTC libraries which then caused problems when porting back to conventional C code for ConVEX and keeping the API the same across both platforms.
The ROBOTC system of intrinsics is not for use by end users.
This is really kludgy (and untested) but should work with up to 16 numbers which I think is sufficient for most use cases. But if you need more, it’s pretty simple to modify.
const int MAX_FLOATS = 16;
float findMaxFloat(int a_0,
int a_1,
int a_2 = 0,
int a_3 = 0,
int a_4 = 0,
int a_5 = 0,
int a_6 = 0,
int a_7 = 0,
int a_8 = 0,
int a_9 = 0,
int a_10 = 0,
int a_11 = 0,
int a_12 = 0,
int a_13 = 0,
int a_14 = 0,
int a_15 = 0)
{
int array[MAX_FLOATS];
array[0] = a_0;
array[1] = a_1;
array[2] = a_2;
array[3] = a_3;
array[4] = a_4;
array[5] = a_5;
array[6] = a_6;
array[7] = a_7;
array[8] = a_8;
array[9] = a_9;
array[10] = a_10;
array[11] = a_11;
array[12] = a_12;
array[13] = a_13;
array[14] = a_14;
array[15] = a_15;
float largest = 0;
for (int i = 0; i < MAX_FLOATS; i++)
{
if (array* > largest)
largest = array*;
}
return largest;
}
This code doesn’t require that you pass in the number of arguments like the original did.
Passing in an array (well, a pointer to an array) would probably be the more correct way to do this, but this way will let you use the cleaner syntax in the actual client code without needing to declare an array, etc, etc.
**Edit: **Looks like jpearman beat me to it, but this is an actual implementation of what you wanted to do using the concept he described.
I was afraid I was going to have to do it the “normal” way. Not that big of a deal, since I doubt I’ll ever need to sort more than n-variables at a time, where n is a reasonable number
. Thanks for the info!