7 Replies - 1387 Views - Last Post: 07 March 2011 - 08:14 AM Rate Topic: -----

#1 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: two dimensional array

Posted 07 March 2011 - 07:39 AM

View Postsinister18, 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.
Was This Post Helpful? 0
  • +
  • -

#7 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: two dimensional array

Posted 07 March 2011 - 08:14 AM

View Postsinister18, 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
confusion with ordinary addition.
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.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1