Reputation: 627
Hi I tried the following code:
int *a=&matrice[4][0];
cout<<*a<<"\n\n";
int *b;
b = &matrice[4,4];
Where matrice is defined like this: int matrice[5][5];
and I filled it with some normal int values.
The first assignment works but the second doesn't with the error :
a value of type "int (*)[5]" cannot be assigned to an entity of type "int * "
.
I expected both of them to give me an error at compile time.
Could you explain why there is this different behavior ?
Upvotes: 2
Views: 155
Reputation: 105992
&matrice[4][0]
is of type int *
and you are assigning it to a
which is also of type int *
. Nothing wrong in this. You will not get any type of error.
matrice[4,4]
is not representing a 2D matrix rather it evaluates to matrice[4]
because of the effect of the ,
operator and &matrice[4]
is of type int(*)[5]
. Try this;
b = &matrice[4][4];
Upvotes: 2
Reputation: 8839
In the first case, you are assigning a pointer address to a
. matrice
is internally defined as a pointer to an array of pointers and you are getting the address of one such vector into a
.
The second one is using the comma operator inside the index reference. That is incorrect as well.
Upvotes: 0
Reputation: 81384
The expression
&matrice[4,4]
does not do what you have expected, because the expression
4,4
evaluates to 4
. This is an example of the comma operator, which evaluates both operands and returns the second operand.
Upvotes: 7