A. Bolmarcich said:
A. Bolmarcich said:
How is a part of the C++ standard that is about dereferencing an
array relevant to a C++ statement in which there is no dereferencing
of an array?
Because it explains that when you derefernece an array it returns a
pointed
to (n-1) dimensional array.
In order for a part of the C++ standard that is about a dereference
of an array to be relevant to a C++ statement, the statement has to
contain a dereference of an array. The statement
int *p = new int[4]
does not dereference an array.
There are many other sections of the C++ standard that are not
relevant to that statement, such section 5.7 about Additive
Operators. There are some sections that are relevant, such as
section 5.3.4 about New.
The argument is not simply about this statement, it is about what a
pointer
to an array is. Therefore the section of the C++ standards that defines
pointers to arrays is very releveant.
My comment was simply about a C++ statement. I wrote that part of
the C++ standard about dereferencing an array is not relevant to the
statement
int *p = new int[4];
because that statement does not dereference an array.
In addition, the statement does not involve a pointer to an array.
The statement declares a pointer to an int. That pointer is
initialized with a C++ expression that according to the C++
standard returns a pointer to an int (see paragraph 5 of section
5.3.4 of the standard about New, which I quoted it in my first post
in this thread of posts).
Nonsense that pointer simply does point to an array. Obviously its type is
pointer to int, but this does not mean it cannot point to an array of int.
Since you have stated that a pointer of type int* cannot point to an array
the rellevant sections of the standards, that address dereferencing pointers
to arrays, are applicable.
Why do you think, with three pointers to exactly the same place,
they
point
to different things?
Its exactly the opposite of what you say because:
int arr[6]; /*r-value , can only point to the first element*/
int* parr=arr; /*l-value , can point to any element*/
int (*pparr)[6] = &arr; /*l-value, can only point to 1st element or
1
past
end.*/
They all initially point to the same address, but they have
different
types.
arr has type array of 6 int. parr has type pointer to int. pparr
has
type
pointer to array of 6 int.
They are all int types
arr[6]; /* returns int */
*parr; /*returns int */
*pparr; /*returns int[], which decays to int* */
The third is simply another level of indirection.
It is the variables parr and pparr that are being initialized, not
the result of dereferencing them. Those variables have different
types: pointer to int and pointer to array of 6 int, respectively.
Neither of those variables is type int.
It doesn't matter if its TYPE is int*****.
Its just a different level of indirection. The type of the array is
INT.
The type int***** is not the same as type int. Values with different
levels of indirection to the same type have different types.
Why would anyone suggest it was the same type? This is only your
ludicrous
suggestion. We are talking about arrays and different levels of
indirection,
the typeof the array is int, not int*** or int(*)[x][y][z].
An array of int* is not the same as a 2d array of int.
You are the one who suggested that they were the same type by
writing: "They all are int types" and "It doesn't matter if the
TYPE is int*****."
I'm saying they are all arrays of the same type i.e:
int arr1[3][4]; /*Array of int type*/
int arr2[6]; /*Array of int*/
int* p1 = new int[5]; /*Array of int*/
int (*p2)[6] = new int[5][6]; /*Array of int*/
They are all int-type arrays and the identifier in each case, no matter what
type that is, points to an array of int.
[snip]
But you obviously don't understand that an int* can point to a single
int,
or an array of int's. Thus it seems you have have some difficulties with
understanding common sense.
A pointer type does not define what is pointed to, what is pointed to is
defined by allocation.
What I understand is that in C++ a pointer to an int points to an int
and not to an array of int.
This is wrong. An int* can point to an array of ints.
It can actually point to almost anything but within reason its either
uninitialised, null , points to int or points to array of int.
A pointer to an int and a pointer to an
array of int are different types.
No they're not. Your thinking about pointer types, not what is pointed to. I
have already said a pointer type doesn't define what is pointed to but I
will repeat it.
It is not a matter of common sense;
it is a matter of what the term "points to" means in C++. Here is
paragraph 1 of section 5.3.1 of the C++ standard:
I know what points to means I dont need to reference the standard to find
out.
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.]
According to the C++ standard, the pointer type determines the
type of the result of dereferencing the pointer
So the C++ standards are relevant now when talking about dereferencing
pointers?
If you read the above in conjuction with the relevant section on arrays you
will understand.
According the the C++ standard, see the preceeding quote from the
standard, the type of the result of dereferencing a pointer depends
on the type of the pointer. The result type does not depend on the
type used when the object was allocated.
This does not mean the an int* cannot point to an array, If you read the
standards properly you will understand why a pointer to an array must point
to , the pointed to (n-1) diemnsional array.
And I though you said the standards were irellevant because we were not
talking aobut dereferencing a pointer.
[snip]
What exactly does SILENTLY snipped mean? Does snipping produce sound on
your
PC?
Silently snipped means removing lines of the post being followed-up
to without indicating that the lines have been removed, such as by
including "[snip]" in the followup.
A pointer to the first element of the array is not the same as a
pointer
to
the array. The former has type pointer to T, that latter has type
pointer
to array of N T. If they were the same, given the declaration
A pointer to the first element of an array, is a pointer to the array.
int *pi, (*pai)[1];
the assignment
pi = pai;
would be allowed, but it is not allowed.
You obviously do not understand the difference in level sof
indirection.
What C++ compiler have you used that does not give a diagnostic
message about the assignment statement?
Your code is just wrong. I don't know what its supposed to do.
What it tries to do is assign a pointer to an array of int to a
pointer to an int.
Well thats just wrong because the pointer types are different levels if
indirection. You convert an array into a pointer like so :
int arr[5];
int* p = arr;
You don't take the address of an array , unless you want a different level
of indirection. Arrays are implicitly converted to pointers.
The fact that it is not allowed by the C++
standard demonstrates that a pointer to an int cannot not point
to an array of int.
It is allowed but it needs a cast.
It can point to an element of an array of int.
The statement
pi = &((*pai)[0]);
is allowed. It assigns a pointer to an element of an array of int
to a pointer to an int.
You are either deliberately or unknowingly writing bad code.
[snip]
If it points to the first element *OF THE ARRAY* then, by definition
,
it
points to the array.
Such a definition ignores the fact that C++ pointers are typed. The
"it" that points is a pointer with a type. A pointer to an element of
an array and a pointer to an array are two different types of
pointers.
No it ignores no facts, it acknowledges the relevant sections of the
standards that you think are irrelevant.
The relevant sections of the standard for the statement
pi = pai;
where pi and pai are declared with
int *pi, (*pai)[1];
are the one about assignment operators (5.17) and the one about
implict conversions (4) that it refers to. According to those
sections, that assignment, which tries have a pointer to int point
to an array, is not allowed. If pi and pai had the same type, the
assignment would be allowed.
What is this nonsense about , your code is trash dude and you can't even
accept what is really the relevant sections of the standard, the sections
about arrays.
The fact that C++ compilers reject the code demonstrates that a
pointer to an array of int cannot be assigned to a pointer to int.
In other words, a pointer to int cannot point to an array of int.
What paragraph of the C++ standard states otherwise?
Its your code that is rejected by compilers so its your understanding that
must be wrong.
I fully understand the difference between the types:
int(*)[size] and int*.
I also understand that both can point to arrays, you cannot understand
this
though . You think only one type can point to an array which is ofc
against
the views of all the C++ experts.
If you fully understood the difference between those types in C++,
you would understand that a pointer to int cannot point to an array
of int. A term, such as "point to", used in the context of a
a programming language does not necessarily have the same meaning
that it has in other contexts.
As you refuse to accept all reason and common sense I can only appeal to
authority. If people like Bjarne Stroustrup say that an int* can point to
a
single int or an array of int, and you say otherwise, who are we to
believe
is correct? You or Bjarne Stroustrup?
No one needs to believe anything that is not supported by what is
in the C++ standard or by compliers that comply to the standard.
Given the declaraion
int *pi, (*pai)[1];
the statement
pi = pai;
which attempts to have a pointer to an int point to the array of int
(pointed to by the variable pai) is not allowed. C++ compilers will
issue a diagnosic message for the assignment statement.
That piece of bad code you have produced somehow suggests that Bjarne
Stroustrup is wrong does it?
If the C++ standard states that , when dereferenced, an array(or pointer
to
an array) returns the pointed to (n-1)dimension array. Then who are we
to
believe, you or the C++ standard?
Right, in the statement
int a2i[3][5], (*pa1i)[5] = a2i;
evaluating a2i in the initialization expression returns a pointer to
a 1-dimensional array. After the initialization pa1i points to that
1-dimensional array, not to a 2-dimensional array.
a2i is implicitly converted to a pointer. The pointed to array is a 2d
array, not a 1dim array. Where the above standards quote applies is if you
derefernece either a2i or pa1i, eg:
*pa1i;
or
pa1i[0];
This is dereferencing the pointer to the array, and the results of this
dereference is an array of (n-1) dimensions , eg:
int[5];
As you can see the standard agrees perfectly with my interpretation.
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).
You are creating another level of indirection, because arr already is(or
converts to) a pointer, under the hood. You compare this with a pointer to a
non array object, eg:
int arr[6];
int (*p)[6] = &arr; /*Taking address of a pointer.*/
int x =5;
int* p_x=&x; /*Creating a pointer */
You think the above is the same but its not, what you are doing is similar
to this:
int** pp = &p_x; /*Taking address of a pointer*/
Exactly where in the C++ standard is a statement that a pointer of
another type can "point to an array", as opposed to a pointer whose
type is the same as an array element type being able to point to an
element of that array?
You refuse to accept what the C++ standard says about arrays, you said it
was irrelevant because the statement:
int* p = new int[7];
does not contain any dereference.
I accept what the C++ standard says about arrays. I said that parts
of the C++ standard about *dereferencing* are not relevant to a
statement that does not contain dereferencing (I have emphasized the
word *dereferencing* because you omitted it).
Well you have posted stuff from the standards about dereferencing pointers.
I have answered your questions and cited parts of the C++ standard
relevant to example C++ statements. I don't know why you don't
similarly answer my questions, including the one immediately before
the last lines of your post.
I have answered any questins you have asked.