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

Initializing two dimensional array
http://www.robotc.net/forums/viewtopic.php?f=1&t=5765
Page 1 of 1

Author:  Dennis Mabrey [ Fri Apr 26, 2013 6:09 pm ]
Post subject:  Initializing two dimensional array

Having a problem initializing a two dimensional array. The following code will not compile:
Code:
int adjlist[5][4] =
{
  {1,5},
  {0,2,3}
};

task main()
{
  // do nada for this sample
}


The compile time error says "**Error**:Wrong number of initialization elements for second array bound of 'adjlist'"

However, this code complies just fine.
Code:
int adjlist[5][4] =
{
  {1,5},
  {0,2}    //  <--- took out the 3
};

task main()
{
  // do nada for this sample
}


I am using RobotC version 3.60.

Author:  Dennis Mabrey [ Fri Apr 26, 2013 6:22 pm ]
Post subject:  Re: Initializing two dimensional array

But wait.... there's more.

It appears that RobotC wants all rows to be of equal length. So for example this code compiles just fine

Code:
int adjlist[5][4] =
{
  {1,5,3},
  {0,2,3},
  {4,5,3}
};

task main()
{
  // do nada for this sample;
}


As long as each row has an equal number of elements it will compile fine.

Author:  Coder A [ Fri Apr 26, 2013 8:04 pm ]
Post subject:  Re: Initializing two dimensional array

Two dimensional arrays must have rectangular boundaries. The numbers in the [square brackets] determine the length and width of the array. However, if the array's bounds are not equal to the bounds of the array as set in the {curly braces}, then the curlies trump. This means that in the second code, the array is actually set up as a [2][2] array, because even though you wanted a [5][4] array, the variables formed just a 2x2 one. In the third code, you set up a 3x3 in the same way.

The problem with your first code is the compiler does not know whether you want a [2][3] or a [2][2] array. also notice that in none of your code does the compiler set up a [5][4] array. This can be fixed by choosing a value that will not ever naturally appear in your array, preferably -1, and setting the rest of your values to it, as shown:

Code:

int adjlist[5][4]
{
  { 1, 5,-1,-1},\
  { 0, 2, 3,-1}, \
  {-1,-1,-1,-1}, |--most of the array is set as -1 because we have yet to actually use the rest.
  {-1,-1,-1,-1}, /
  {-1,-1,-1,-1} /
};




Does that help you?

Author:  Dennis Mabrey [ Wed May 01, 2013 10:37 am ]
Post subject:  Re: Initializing two dimensional array

Coder A,

As you can see I said the same thing in my second post.

Thanks.

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