views:

91

answers:

2
+2  Q: 

Array and Rvalue

$4.2/1 - "An lvalue or rvalue of type “array ofN T” or “array of unknown bound of T” can be converted to an rvalue of type “pointer to T.” The result is a pointer to the first element of the array."

I am not sure how do we get an rvalue of an array type other than during initialization/declaration?

A: 

You cannot get an rvalue of array type. Arrays can only be lvalues, and whenever they are used in an lvalue they decay to a pointer to the first element.

int array[10];
int * p = array; // [1]

The expression array in [1] is an lvalue of type int (&)[10] that gets converted to an rvalue of type int *p, that is, the rvalue array of N==10 T==int is converted to an lvalue of type pointer to T==int.

David Rodríguez - dribeas
@David Rodríguez - dribeas: Can you please show me an example where the above quote holds good for an Rvalue of Array type?
Chubsdad
Is that the reason why functions cannot return array types ?
Cedric H.
@Cedric H.: In §8.3.5[dcl.fct]/3 the standard specifies that *After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.* That is, the standard specifies that `void foo( char [10] )` is a declaration exactly equivalent to `void foo( char\* )` -- that is for arguments. Then in §8.3.5/6 it says: *Functions shall not have a return typeof type array or function, although they may have a return type of type pointer or reference to such things*. for return types
David Rodríguez - dribeas
@David: Thanks for clarifying this !
Cedric H.
A: 

Would this stand a chance to demonstrate Array Rvalue?

int main(){
 int buf[10][10];

 int (*p)[10] = buf;

 int (*p2)[10] = p;      // LValue to Rvalue conversion of Array type 'p'
}
Chubsdad
Nope... p is not array type... it's a pointer to an array.
Tony