View unanswered posts | View active topics It is currently Fri Oct 31, 2014 11:07 am






Reply to topic  [ 4 posts ] 
Initializing two dimensional array 
Author Message
Rookie

Joined: Mon May 14, 2012 3:24 pm
Posts: 43
Post 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.


Fri Apr 26, 2013 6:09 pm
Profile
Rookie

Joined: Mon May 14, 2012 3:24 pm
Posts: 43
Post 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.


Fri Apr 26, 2013 6:22 pm
Profile
Moderator
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 198
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Post 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?

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot


Fri Apr 26, 2013 8:04 pm
Profile
Rookie

Joined: Mon May 14, 2012 3:24 pm
Posts: 43
Post Re: Initializing two dimensional array
Coder A,

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

Thanks.


Wed May 01, 2013 10:37 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.