# int**

Page 1 of 1

## 6 Replies - 564 Views - Last Post: 11 February 2011 - 03:42 AMRate 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=215307&amp;s=f64ecd5f146c01b146d609b1786e9974&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 livium

Reputation: 0
• Posts: 543
• Joined: 21-December 08

# int**

Posted 10 February 2011 - 01:07 PM

Why is it that one cannot pass a matrix (defined as a[3][3]) to a function like int**, and can do this with a simple array (defined as a[3]) like int* ?
Is This A Good Question/Topic? 0

## Replies To: int**

### #2 chinchang

• Indie Game Developer

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

## Re: int**

Posted 10 February 2011 - 01:13 PM

Because even in a[3][3], a is still a pointer to an integer. i.e. int* not int**

EDIT : a is a "pointer to a row of integers" not int*

This post has been edited by chinchang: 10 February 2011 - 01:26 PM

### #3 livium

Reputation: 0
• Posts: 543
• Joined: 21-December 08

## Re: int**

Posted 10 February 2011 - 01:19 PM

So are you telling me that a in a[3][3] is not int** but int*?
I did not knew that.
This is a shock to me!
So only when one declares a matrix dinamicaly a is int**.

But, following from this, when one declares a[3], then a is an int?
Isnt't it a pointer to int?

### #4 chinchang

• Indie Game Developer

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

## Re: int**

Posted 10 February 2011 - 01:24 PM

Yes. In a[3] a is a pointer to an int.

Try this :

```int m[3][3];
m[0][0] = 5;

cout<<m;
cout<<&m;
cout<<*m;
```

m is a pointer to a pointer to an integer but it can't be treated as a 2D array. That is why you can't pass it to a function which requires a 2D array.

This post has been edited by chinchang: 10 February 2011 - 01:32 PM

### #5 livium

Reputation: 0
• Posts: 543
• Joined: 21-December 08

## Re: int**

Posted 10 February 2011 - 01:34 PM

### #6 Oler1s

• D.I.C Lover

Reputation: 1396
• Posts: 3,884
• Joined: 04-June 09

## Re: int**

Posted 10 February 2011 - 02:45 PM

POPULAR

Not quite right information here.

int a[3][3]; has type int [3][3]. Not int *. Not int **. Not int [3]. Not char. Just int [3][3] (array of size 3 of array of size 3 of int). This is not difficult.

int ** a; has type int **. Not int *. Not int [3]. Not int [5]. Not int [3][3]. Not anything else. This is not hard either.

int (*a)[3] has type pointer to int [3]. Not int *. Not int **. Not int [3]. int (*) [3] (pointer to array of size 3 of int).

Now, C and C++ have something called array to pointer decay. When you refer to an array, syntactically, it's like you have a pointer to the first element. So if you write a somewhere in your code, it's like you wrote &a[0] instead. For nearly all expressions, this decay happens.

What is &a[0]? If a has type int [3][3], a[0] has type int[3]. Then a pointer to that element has type int (*) [3]. Not int **. Not int *. Not int [3]. Not anything else.

Where's an example of this array to pointer decay? Argument passing.

foo(a) means that it's like you passed a variable of type int (*)[3]. Not int *. Not int **. Not int *[3][3].

In function parameter declarations, int (*) is the same as int []. That is why, for example, char *argv[] is the same as char **argv. So going back to your original question:

Quote

Why is it that one cannot pass a matrix (defined as a[3][3]) to a function like int**
Because when you pass an int[3][3], it decays to int (*)[3], which is not the same as int **. There's a type mismatch.

When you pass something of type int [3], it decays to int *, which is why the correct function argument type is int *.

This post has been edited by Oler1s: 10 February 2011 - 02:49 PM

### #7 livium

Reputation: 0
• Posts: 543
• Joined: 21-December 08

## Re: int**

Posted 11 February 2011 - 03:42 AM

Oler1s, these are great explanations!
Now I understand!
Thank you so much for your effort in writing this!