# two dimensional array

Page 1 of 1

## 7 Replies - 1957 Views - Last Post: 07 March 2011 - 08:14 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=220542&amp;s=fc87eb1eeb902327894741103841bbaa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sinister18

Reputation: 0
• Posts: 13
• Joined: 27-November 10

# two dimensional array

Posted 06 March 2011 - 05:38 PM

a problem that one friend arised in the class is that: suppose you have two dimensional array such as the follwoing:
```char board[3][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};

```

in our case board = &board[0][0]
so why we need two indirection such as **board in order to get the value '1' instead of just using one indirection such as *board?

This post has been edited by JackOfAllTrades: 06 March 2011 - 05:41 PM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: two dimensional array

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: two dimensional array

Posted 06 March 2011 - 07:33 PM

Because board is an array of three char arrays.
The first level of indirection gets you into the "outer" array, to the first one of the "inner" arrays.

*board gets you the first array: {'1','2','3'} so you could write, for example,
```char *first = *board;
char one = *first;
cout << one << endl; //prints 1

```

Now first is an array of three chars, so you can also write:
```char *first = *board;
cout << first[0] << endl; //prints 1

```

**board gets you the first element of the first array, so you could also write
```char one = **board;
cout << one << endl;

```

This post has been edited by r.stiltskin: 06 March 2011 - 07:34 PM

### #3 sinister18

Reputation: 0
• Posts: 13
• Joined: 27-November 10

## Re: two dimensional array

Posted 07 March 2011 - 04:43 AM

but board = &board[0][0...doesn't indirection takes the address as a parameter and gives it's value?

### #4 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

## Re: two dimensional array

Posted 07 March 2011 - 06:34 AM

There is something called as "Decay convention" which r.stiltskin explained.

Deferencing always gives the first element of the array. In a 2D-array the first element is not a[0][0] but a[0] because of this :

Array a[2][3] =>
a[0] => { array of 3 elements }
a[1] => { array of 3 elements }

As shown above, first element of a is a[0] whose first element is a[0][0].

You might want to look at this.

This post has been edited by chinchang: 07 March 2011 - 06:35 AM

### #5 sinister18

Reputation: 0
• Posts: 13
• Joined: 27-November 10

## Re: two dimensional array

Posted 07 March 2011 - 07:32 AM

so you aretelling me that when deferencing an array, you gget the first element regardless of it's address?

### #6 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

## Re: two dimensional array

Posted 07 March 2011 - 07:39 AM

sinister18, on 07 March 2011 - 08:02 PM, said:

so you aretelling me that when deferencing an array, you gget the first element regardless of it's address?

Yes, dereferencing an array gives its first element.

### #7 sinister18

Reputation: 0
• Posts: 13
• Joined: 27-November 10

## Re: two dimensional array

Posted 07 March 2011 - 08:03 AM

can you show me please where it appears in the link you sent me?

### #8 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 727
• Joined: 22-December 08

## Re: two dimensional array

Posted 07 March 2011 - 08:14 AM

sinister18, on 07 March 2011 - 08:33 PM, said:

can you show me please where it appears in the link you sent me?

This is it :

Quote

The K&R method of reducing arrays to pointers
---------------------------------------------
K&R tried to create a unified treatment of arrays and pointers, one that
would expose rather than hide the array equation in the compiler's code.
They found an elegant solution, albeit a bit complicated. The "ugly"
array equation is replaced in their formulation by four rules:

1) An array of dimension N is a 1D array with
elements that are arrays of dimension N-1.

2) Pointer addition is defined by:

ptr # n = ptr + n * size(type-pointed-into)

"#" denotes here pointer addition to avoid
The function "size()" returns object's sizes.

3) The famous "decay convention": an array is
treated as a pointer that points to the
first element of the array.

The decay convention shouldn't be applied
more than once to the same object.

4) Taking a subscript with value i is equivalent
to the operation: "pointer-add i and then
type-dereference the sum", i.e.

xxx[i] = *(xxx # i)

When rule #4 + rule #3 are applied recursively
(this is the case of a multi-dimensional array),
only the data type is dereferenced and not the
pointer's value, except on the last step.