On Oct 3, 6:22 am, venkatagmail <chikkub...@gmail.com> wrote:
> >>>"a is of array type, which is bound to a fixed address. so you can't
> change a to point to another one. when you pass an array as a
> parameter
> for a function, here a decays to type /int* const/, so you can convert
> it to /int*&/."<<<
I don't like the word "decays" here (although I know it is quite
common). It implicitly converts to, in the same way an int will
implicitly convert to a double. The important thing to remember
(here) is that the pointer is a new, temporary object.
> Barry, do you suggest that, I can convert array a bound to fixed
> address which decays to /int* const/ to type /int*&/ ??
That's not the problem. The types match. The problem is that
you cannot initialize a reference to non-const with a temporary,
and the result of your conversion is a temporary. This is a
separate rule, outside the type system, which applies to the
initialization of a reference.
> i.e., if I create an array int a[] = {1,2}; it is bound to a
> fixed address. If I pass it to a function fun2(int *&a2) it
> gives me an error!! Whereas I create another ptr /int* aPtr =
> a/, I can then pass it as this can now point to any integer
> and is not a constant value?
Because a named variable is not a temporary. (The standard
actually speaks in terms of rvalues and lvalues, but it comes
out to the same thing here, at least with non class types: a
temporary is an rvalue, i.e. a pure value, without an address.)
[..]
> What about the memory contents and how these are allocated for the
> three types of passing input arguments fun1(int a1[]) or fun1(int *a1)
> and fun2(int *&a2) ?? how different are these in terms how memory is
> allocated and assigned?
The first two are identical: "fun1(int* a)". Given a pointer to
an int (whether it is the result of an implicit array to pointer
conversion, or otherwise, makes no difference), the compiler
uses the value of the argument (the rvalue) to initialize the
function argument (which may be in a register, or on the stack).
The third says that you are passing a reference to the pointer,
and that the pointer may be changed via this reference.
Typically, the compiler implements this by passing the address
of the argument---the address of the pointer, here. Since the
result of a conversion is a pure value, the compiler would have
to artificially generate an object (with pointer type) to hold
it, and pass the address of this object. In fact, the standard
forbids this.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34