# Multidimensional Arrays

Page 1 of 1

## 7 Replies - 983 Views - Last Post: 20 February 2013 - 09:52 PMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312870&amp;s=0f170f81abd89824add166aac866fafe&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AwesomeProgrammer

Reputation: 0
• Posts: 3
• Joined: 14-February 13

# Multidimensional Arrays

Posted 20 February 2013 - 12:24 PM

Hello, I have recently been trying to make a new program making multidimensional arrays! Here is my code:
``` #include <iostream>

using namespace std;

int main()
{

const int MAX_ROWS = 3;
const int MAX_COLUMNS = 3;

char table [MAX_ROWS][MAX_COLUMNS] = {
{  {'O', 'X', 'O'},
{'O', 'X', 'X'},
{'X', 'O', 'X'} }
};

for (int i = 0;i<MAX_ROWS;i++){
for (int j = 0; j<MAX_COLUMNS;j++) {
cout << table[i][j];
}
cout << endl;
}

return 0;
}

```

I get the error: Excess elements in scalar initializer on line 12 on the X.
Could anybody help?

Is This A Good Question/Topic? 0

## Replies To: Multidimensional Arrays

### #2 jimblumberg

Reputation: 4912
• Posts: 15,351
• Joined: 25-December 09

## Re: Multidimensional Arrays

Posted 20 February 2013 - 12:30 PM

It looks like maybe you have too many braces in your initializer.

Jim

### #3 mojo666

Reputation: 397
• Posts: 859
• Joined: 27-June 09

## Re: Multidimensional Arrays

Posted 20 February 2013 - 12:30 PM

You have an extra set of brackets.

```    char table [MAX_ROWS][MAX_COLUMNS] = {
{  {'O', 'X', 'O'},
{'O', 'X', 'X'},
{'X', 'O', 'X'} }
};
//should be
char table [MAX_ROWS][MAX_COLUMNS] = {
{'O', 'X', 'O'},
{'O', 'X', 'X'},
{'X', 'O', 'X'}
};
```

### #4 AwesomeProgrammer

Reputation: 0
• Posts: 3
• Joined: 14-February 13

## Re: Multidimensional Arrays

Posted 20 February 2013 - 12:41 PM

Ok thanks that solve that problem, however, this code:
```
#include <iostream>

using namespace std;

int main()
{

const int MAX_ROWS = 3;
const int MAX_COLUMNS = 3;

char table [MAX_ROWS][MAX_COLUMNS] = {
{  'O', 'X', 'O',
'O', 'X', 'X',
'X', 'O', 'X' }
};

for (int i = 0;i<MAX_ROWS;i++){
for (int j = 0; j<MAX_COLUMNS;j++) {
cout << table[i][j];
}
cout << endl;
}

return 0;
}

```

gets an error message: Excess elements in array initializer on line 13 on the O. Could anybody help again?

Oops sorry i should of deleted that 4th post.

Sorry for post 4! I didn't read the posts fully Thanks!

### #5 ipoood

Reputation: 0
• Posts: 22
• Joined: 30-March 12

## Re: Multidimensional Arrays

Posted 20 February 2013 - 04:46 PM

You don't have enough brackets now.

```char table [MAX_ROWS][MAX_COLUMNS] = {

{  'O', 'X', 'O',

'O', 'X', 'X',

'X', 'O', 'X' }

};

```

should be like this:

```char table[MAX_ROWS][MAX_COLUMNS] = {
{'O', 'X', 'O'},

{'O', 'X', 'X'},

{'X', 'O', 'X'}

}

```

Each row needs to have its own set of brackets with a comma in between each one.

### #6 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Multidimensional Arrays

Posted 20 February 2013 - 08:11 PM

Quote

Each row needs to have its own set of brackets with a comma in between each one.

Not every row needs it's own set of brackets, it is sufficient however. Since a 2D array is continuously addressed in memory, you can omit the row brackets.

```char table[MAX_ROWS][MAX_COLUMNS] = {
'O', 'X', 'O',

'O', 'X', 'X',

'X', 'O', 'X'
}

```

### #7 jimblumberg

Reputation: 4912
• Posts: 15,351
• Joined: 25-December 09

## Re: Multidimensional Arrays

Posted 20 February 2013 - 09:22 PM

Actually if I try to leave out the row braces I get these warnings:

Quote

main.cpp||In function ‘int main()’:|
main.cpp|13|warning: missing braces around initializer for ‘char [3]’ [-Wmissing-braces]|
main.cpp|13|warning: missing braces around initializer for ‘char [3]’ [-Wmissing-braces]|
main.cpp|13|warning: missing braces around initializer for ‘char [3]’ [-Wmissing-braces]|

Jim

### #8 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Multidimensional Arrays

Posted 20 February 2013 - 09:52 PM

Quote

Actually if I try to leave out the row braces I get these warnings:

I do if I compile with -Wall. I also get this warning with statements like
```int arr[3][3] = {0};

```

Statements like this do not go against the standard which states that the left out aggregate shall be initialized implicitly.