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* ?
int**
Page 1 of 16 Replies - 287 Views - Last Post: 11 February 2011 - 03:42 AM
Replies To: int**
#2
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*
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
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?
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
Re: int**
Posted 10 February 2011 - 01:24 PM
Yes. In a[3] a is a pointer to an int.
Try this :
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.
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
#5
Re: int**
Posted 10 February 2011 - 01:34 PM
Ok, thank you very much for your answers!
#6
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:
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 *.
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**
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
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!
Now I understand!
Thank you so much for your effort in writing this!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|