Re: Problem with array objects
On 2011-04-12, Paul <pchristor@yahoo.co.uk> wrote:
[snip]
The argument is about what a pointer to an array points to, not what happens
when its dereferenced.
If you want to talk about what happens when pointer is dereferenced then
refer to the relevant section in the standards that fully explains what
happens when a pointer to an array is dereferenced.
When dereferenced, a pointer to an array yields an (n-1) dimensional array.
A pointer to an array points to an array. What happens when a
pointer is dereferenced is given in paragraph 1 of section 5.3.1
(Unary operators):
The unary * operator performs indirection: the expression to which
it is applied shall be a pointer to an object type, or a pointer
to a function type and the result is an lvalue referring to the
object or function to which the expression points. If the type of
the expression is pointer to T, the type of the result is T.
[Note: a pointer to an incomplete type (other than cv void ) can
be dereferenced. The lvalue thus obtained can be used in limited
ways (to initialize a reference, for example); this lvalue must
not be converted to an rvalue, see 4.1.]
Because dereferencing a pointer to T results in a T, dereferencing a
pointer to an array results in an array.
It is important to undestand what the C++ standard means by a
pointer to an array. Here is a example from section 8.1 (Type
Names) of the C++ standard:
int // int i
int * // int *pi
int *[3] // int *p[3]
int (*)[3] // int (*p3i)[3]
int *() // int *f()
int (*)(double) // int (*pf)(double)
name respectively the types "int," "pointer to int," "array of 3
pointers to int," "pointer to array of 3 int," "function of (no
parameters) returning pointer to int," and "pointer to a function
of (double) returning int.
The declaration
int (*p3i)[3];
declares a pointer to an array. The declaration
int *pi;
declares a pointer to an int.
The output from the following program confirms that.
#include <iostream>
#include <typeinfo>
int main() {
int a3i[3], *pi=a3i, (*p3i)[3]=&a3i;
std::cout<<"typeid( pi )="<<typeid( pi ).name()<<std::endl;
std::cout<<"typeid(*pi )="<<typeid(*pi ).name()<<std::endl;
std::cout<<"typeid( p3i)="<<typeid( p3i).name()<<std::endl;
std::cout<<"typeid(*p3i)="<<typeid(*p3i).name()<<std::endl;
}
The idiotic thing is when I initially mentioned this , you said it was not
relevant section of the standards because it was speaking about
dereferencing a pointer to an array. It is in fact exactly the relevant
section of the standard that suits this argument.
Here is what I wrote (near the bottom of Message-ID:
<slrnios40v.19l0.aggedor@earl-grey.cloud9.net>)
As far as I can tell there in no recent thread titled "You missed
something"; there is one titled "You snipped something". Quotes
you posted there, such as
"If the * operator, either explicitly or implicitly as a result of
subscripting, is applied to this pointer, the result is the pointedto
(n - 1 )dimensional array"
and
"the subscript operator [] is interpreted in such a way that E1[E2]
is identical to *((E1)+(E2))"
have nothing to do with declarations like
int *p = new int[4]
that my posts have been about.
Because that declaration does not contain either the * operator or
the subscript operator, parts of the C++ standard about those
operators are not relevant to that declaration.
That declaration initializes a pointer to int, named p, with the
pointer to int returned by "new int[4]". "new int[4]" returns an
pointer to int according to paragraph 1 of section 5.3.4 (New) of
the C++ standard. That paragraph ends with:
If the entity is a nonarray object, the new-expression returns
a pointer to the object created. If it is an array, the
new-expression returns a pointer to the initial element of
the array.
That is what is in the C++ standard; I accept exactly what is in
the standard.
[snip]