6 Replies - 378 Views - Last Post: 11 February 2011 - 03:42 AM Rate Topic: ***** 1 Votes

#1 livium  Icon User is offline

  • D.I.C Addict

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

  • Indie Game Developer
  • member icon

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

Was This Post Helpful? 0
  • +
  • -

#3 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • 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?
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: 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;


Sorry for my last reply.

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.


Read more here.

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

Was This Post Helpful? 1
  • +
  • -

#5 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: int**

Posted 10 February 2011 - 01:34 PM

Ok, thank you very much for your answers!
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • 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

Was This Post Helpful? 6
  • +
  • -

#7 livium  Icon User is offline

  • D.I.C Addict

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

Page 1 of 1