P
Paul
Thats the pointer-types, not what it points to.A. Bolmarcich said:[snip]
If you take pa1i and consider it a pointer to a 1d array, this fails
because *pa1i does not produce the pointed to (n-1) dimensional
array,
which
would be a single int( a zero dim array).
Nothing fails here by considering pa1i to be a pointer to a
1-dimensional array, which is what it is. Evaluating *pa1i results
in a 1-dimensional array,
One final thing , that does fail because it doesn'tt comply with the
rules.
A pointed to (n-1) dim array, cannot be the same for a 1dim and 2dim
array.
(2-1) != (1-1)
I never claimed that a pointed to (n-1) dim array was the same for a
1dim and 2dim array. With the omitted part of a sentence restored,
what I wrote was
Nothing fails here by considering pa1i to be a pointer to a
1-dimensional array, which is what it is. Evaluating *pa1i results
in a 1-dimensional array, which is implicitly converted to a pointer
to the first element of that array.
pa1i was declared as a pointer to a 2d array with:
int a2i[3][5], (*pa1i)[5] = a2i;
In that declaration pa1i is declared as a pointer to a 1-dimensional
array. Here is an 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.
If you interpret this as saying pa1i does not point to a 2d array then you
have some serious intellectual problems.
No it doesn't the standard clearly says that is a TYPE. The standard doesAccording to the C++ standard, "int (*p3i)[3]" declares a "pointer
to an array of 3 int". That array has one dimension.
not define that it points to a 1d array. The standard actually implies that
type points to a 2d array, in the section about pointers to arrays..
The output demonstrates its an array , not a pointer.Dereferencing pa1i returns a 1d array, which is fine if n==2 because
(n-1)
== 1
If you consider it to be 1d array then n==1, and (n-1) !=1.
std::cout << typeid(*pa1i).name ;
outputs int[5]. There is no implicit conversion to pointer unless it's
further dereferenced i.e: pali[0][0].
There is no implicit conversion in a typeid expression. According
to paragraph 3 of section 5.2.8 (Type identification) of the C++
standard:
When typeid is applied to an expression other than an lvalue of
a polymorphic class type, the result refers to a type_info object
representing the static type of the expression. Lvalue-to-rvalue
(4.1), array-to-pointer (4.2), and function-to-pointer (4.3)
conversions are not applied to the expression. If the type of
the expression is a class type, the class shall be
completely-defined. The expression is not evaluated.
The output "int[5]" demonstrates that pali is a pointer to a 1-dimensional
array. In a context where the expression *pa1i is evaluated, the
resulting
1-dimensional array undergoes array-to-pointer conversion, resulting in a
pointer to int.
The type int[5] is not a pointer. As I said this is not implicitly converted
to a pointer until its further dereferenced. And when it is converted to a
pointer it will be pointer-type: int*, not int (*)[5].
It is initialised to point to a 2d array, with:What you omitted from what I wrote was: "which is implicitly converted to
a
pointer to the first element of that array". The initial value in the
declaration of pali is a pointer to a 1-dimensional array.
int a2i[3][5], (*pa1i)[5] = a2i;
a2i is a 2d array , not a 1d array.
An array is defined at allocation, not by a pointer that points to it.