Robotc arrays

So I’ve been trying to work with arrays in robotc and I’ve run into some problems. I have the following code:


string test = "test string";
int length = strlen(test);
string array[length];

task main() {
    for (int i = 0; i < length; i++) {
        array* = StringGetChar(test, i);
    }
}

I’m trying to create an array with the number of elements as the length of a string, but it is giving me an error when I initialize it. I get no error when I just put a number in the initialization of the string array, but I want to be able to change the string I’m working with and the array to adjust it’s length without me having to do anything. Can anyone give me some pointers on what I’m doing wrong?

Thanks.*

So a couple of things wrong here.

First, you are trying to create an array of strings, not a single array to hold a string. A string is a special ROBOTC type defined somewhere as an array of 20 chars (or was, may be longer these days). Arrays cannot be allocated at run time, you need to decide how large they need to be at compile time. A standard compiler would let you do something like this.

char MyArray] = "hello world";

I forget of ROBOTC allows that (and I’m not near a copy right now), you could try and see.

but we often just decide how big the array needs to be based on knowledge of how the array will be used.

char MyArray[32];

So what are you trying to achieve with this code?

RobotC is not even close to C99, no malloc

You could always write your own implementation of malloc.

OT: I’m on vacation in Sonoma, my son and girlfriend are visiting for the weekend, they are EECS students at Berkeley and homework is to write an implementation of malloc for the operating systems class.

Do you know how I could make a 2D array, kind of like a board . And a way to define Cartesian coordinates for each index


 int twoDimArray]] = {{1,2,3}{4,5,6}}; 

would make a 2-dimensional array-of-int.