Problem with array objects

L

Lasse Reichstein Nielsen

Paul said:
The first element of an array is an element of an array. Nobody said
it was an array.

Yes it is. How can it not point to the array if it points to an
element within the array?

You are using everyday "common" logic here.
If I point at your arm, I also point at you.
That's the everyday meaning of pointing.

This is a technical discussion about a subject (C++) where some common
words have been given more precise meaning.
When medics on the job talk about "shock", they don't mean the same as
the common usage ("I was shocked to discover ..."), but the more
precise medical condition: http://en.wikipedia.org/wiki/Shock_(circulatory)

When computer scientists and programmers talk about "pointing" in C++,
they don't mean the common usage (identifying something by gesturing),
but a more precise and exclusive meaning: having a pointer of a specific
pointer type hold the address of an object of the type pointed to.

So an int pointer points to an int.
An int[4] pointer points to an array of length 4 of ints.
A pointer to the first element of an array of ints points to an int.
It does not point to the array (in the language of C++, which is what
is used here). It can be said to point INTO the array. That's not
the same as pointing to the array, because "pointing to X" has a specific
meaning here.
Nobody said it was. Its a pointer to an array.

No, it's a pointer to the first element. It's a pointer into the array.
It is not the array.
An array is a contiguous array of elements in memory, It can be
"converted" to almost anything.

True, but a variable referring to the array can be converted to a value
that is a pointer to the first element.
If you cannot understand that an pointer to an element within an array
also points to the array, then you need to give up programming.

Nope. In *programming*, that's the exact correct thing to say. It's
different from the common usage of "pointing", but "pointer" in C++
isn't a common word. In this context, it's a technical term with a
specific meaning that doesn't mean what the common word "pointer" does
in everyday language.

Just as we, rightly, ridicule geologist that come into non-geology
groups and insist that a "rock" must be between 2.5 and 5 cm in
diameter, otherwise it's called something else[1], for taking their
tecnical jargon into an everyday discussion, it's also wrong to
take everyday speech into a technical discussion. You can do it, but
you won't be understood, because you use words that mean something
different than what you think in the context where you have put them.

/L
[1] As example, I don't remember the exact details, and details might also
be language dependent.
 
P

Paul

Bo Persson said:
A. Bolmarcich said:
No I have proven that you do not only misinterpret and misquote
authorative texts but you also misunderstand them.
And by proving this I have also proven that you are wrong to say
that p(in the following example) doesn't point to an array:
int* p = new int[4];

Here is paragraph 5 of section 3.4.5, about the new expression,
from the 1998 C++ standard.

"When the allocated object is an array (that is, the
direct-new-declarator syntax is used or the new-type-id or type-id
denotes an array type), the new-expression yields a pointer to the
initial element (if any) of the array. [Note: both new int and new
int [10] have type int* and the type of new int [10] is int
(*)[10].]"

According to the standard, p (in the example) doesn't point to an
array, that is, p does not point to an object with an array type.

What p points to is an int. The value of p is the int* value
returned by "new int[4]". That value is a pointer to the first of
the four contiguous ints that were allocated.


Paul is right and everybody else is wrong, including the standard.

Live with it! :)

The standard agrees with me, and so do all the experts in C+ , you live with
that.
 
P

Paul

Lasse Reichstein Nielsen said:
You are using everyday "common" logic here.
If I point at your arm, I also point at you.
That's the everyday meaning of pointing.

This is a technical discussion about a subject (C++) where some common
words have been given more precise meaning.
When medics on the job talk about "shock", they don't mean the same as
the common usage ("I was shocked to discover ..."), but the more
precise medical condition:
http://en.wikipedia.org/wiki/Shock_(circulatory)

When computer scientists and programmers talk about "pointing" in C++,
they don't mean the common usage (identifying something by gesturing),
but a more precise and exclusive meaning: having a pointer of a specific
pointer type hold the address of an object of the type pointed to.

Bollocks a pointer in computer programming is an indirect accessor to a
memory area.

So STFU, you are wrong and all the experts agree with me and so does the C++
standard.
an int pointer points to an int.
An int[4] pointer points to an array of length 4 of ints.
A pointer to the first element of an array of ints points to an int.
It does not point to the array (in the language of C++, which is what
is used here). It can be said to point INTO the array. That's not
the same as pointing to the array, because "pointing to X" has a specific
meaning here.
<snip bollocks>
 
P

Paul

Lasse Reichstein Nielsen said:
You are using everyday "common" logic here.
If I point at your arm, I also point at you.
That's the everyday meaning of pointing.

This is a technical discussion about a subject (C++) where some common
words have been given more precise meaning.
When medics on the job talk about "shock", they don't mean the same as
the common usage ("I was shocked to discover ..."), but the more
precise medical condition:
http://en.wikipedia.org/wiki/Shock_(circulatory)

When computer scientists and programmers talk about "pointing" in C++,
they don't mean the common usage (identifying something by gesturing),
but a more precise and exclusive meaning: having a pointer of a specific
pointer type hold the address of an object of the type pointed to.

Bollocks a pointer in computer programming is an indirect accessor to a
memory area.

So STFU, you are wrong and all the experts agree with me and so does the C++
standard.
an int pointer points to an int.
An int[4] pointer points to an array of length 4 of ints.
A pointer to the first element of an array of ints points to an int.
It does not point to the array (in the language of C++, which is what
is used here). It can be said to point INTO the array. That's not
the same as pointing to the array, because "pointing to X" has a specific
meaning here.
<snip bollocks>
 
Ö

Öö Tiib

An array is a contiguous array of elements in memory, It can be "converted"
to almost anything.

Similarly a pointer is an address of memory so it can point to almost
anything?
If you cannot understand that an pointer to an element within an array also
points to the array, then you need to give up programming.

If you can not communicate with other people normally then you should
give up doing anything ... since you will inevitably fail. Even if
what you say is smart for them it sounds nonsense.

Paul, i have some yes/no questions about your usage of terms. I put
them into example code as comments. I added some asserts into code
that sort of "prove" that all questions should be answered with "yes".
It is unlikely that you find a platform where the assertions are
violated.

#include <cassert>
struct X { int sole; int bag[5]; bool ok; };
int main()
{
X x = {-1, {0,1,2,3,4}, true};
int* p = &x.sole;
// 1) Does p point to value -1?
assert( *p == -1 );
// 2) Does p point to whole stucture x?
assert( (char*)p == (char*)x );
// 3) Does p also point to int array in that structure?
assert( p == &x.bag[-1] );
++p;
// 4) Since we incremented p is it now an array?
assert( p == x.bag );
}

I think that the words are just symbols of communication.
Communication is possible only if all sides understand the meaning in
same way. So you can not "prove" what some sentence means ... all you
can do is to agree about it with rest of participants in discussion.
 
P

Paul

A. Bolmarcich said:
No it doesn't is says it points to the first element of an array, You are
changing the meaning by adding ... "not an array".. to this.
This misinterpreted meaning is compeletly wrong , see my attached quote,
and
there are lots of other quotes around from people like Bjarne Stroustrup
that state the same.

Yes, it does. Here is the relevant quote from the C++ standard again:
"[Note:
both new int and new int [10] have type int* and the type of new
int[10] is
int (*)[10].]"


This says exactly what I have said , that an int* can point to an int or an
array of int.
And also it confirms that, when dereferenced, a pointer to an array returns
a pointed to (n-1)dimensional array.
The expression "new int [10]" returns a value of type int*. It is a
pointer to
an int. It is not a pointer to an array.
It is a pointer to a 1dimensional array. It is also a pointer to an int
within the array.
The expression "new int[10]"
returns a pointer to an array.

This expression returns a pointer to a 2dimensional array.
I did not change the meaning by adding "not an array".
Yes you did.
I added it to
emphasize that a pointer to an int and a pointer to an array are different
types of pointers.
Not when it's a 1dimensional array.
Accoriding to the
standard "both new int and new int [10] have type int*".
Exaclty , the pointer type int* can point to a single int or an array of
int.

A valid, non null pointer to a type may point only to an object of that
type.
This is rubbish, you disagree with many experts includng Bjarne Stroustrup,
I quote:
<quote ref: http://www2.research.att.com/~bs/glossary.html>
char* - pointer to a char or an array of char.
It may not also point to a different type. A pointer to an int and a
pointer to an array of int are differnt types of pointers.
Not when its a 1dimensional array.
The faq is not a worded as strictly as a specification. What a char*
can
point to is an element of an array of chars, not to an array. Trying to
have a char* point to an array of chars as with

char arr[1];
char *p = &arr;

is a type mismatch.

No its bad programming.
char* p = arr;
is how its done.

In that declaration p is a pointer to a char, not a pointer to an array.
The
initialization expression undergoes the standard array-to-pointer
conversion
described in section 4.2 of the C++ standard. The result of that
conversion
is a pointer of type char* to the first element of the array. A pointer
to
a char is not a pointer to an array; they are different types of pointers.
You disagree with Bjarne Stroustrup and every other C++ programmer, with a
brain, on the planet.
You also disagree with the C++ standards defintion that a dereferenced array
pointer will return the pointed to (n-1)dimension array.
p points to an array of elements not only a single int, if you don't
accept
quotes from recognised autohriities and the C++ standards I don't have
anything more to say.

No, p points to a char; a pointer to an array is a different type of
pointer.
I have posted quotes from the C++ standard relevant to declarations like
"char *p = arr".
See the thread titled "You missed something " for the quotes from the
standard I refer to.

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

It has everthing to do with it , you say int* cannot point to an array but
the standard says different , the standard says that if a pointer to an
array is dereferneced if returns the pointed to (n-1) array.
1-1 = 0
That is, when dereferneced, a pointer to a 1dimensional array will return a
0dimensional array. Which is exactly what int* does.

In your theory what exactly is a pointer to a 1dimensional array, that
conforms to this rule of the standard?
int *p = new int[4]

that my posts have been about.

In the quote "If the * operator ..." you omitted the first sentence of the
paragraph: "A consistent rule is followed for multidimensional arrays."
What
you quoted applies to multidimensional arrays; it does not apply to arrays
of
one dimension.

A "*consistent* rule applies" means "the same rule applies".
 
P

Paul

cg_chas said:
The standard says that p points to an int, not an array.
No it doesn't is says it points to the first element of an array, You
are
changing the meaning by adding ... "not an array".. to this.
This misinterpreted meaning is compeletly wrong , see my attached
quote,
and
there are lots of other quotes around from people like Bjarne
Stroustrup
that state the same.

Yes, it does. Here is the relevant quote from the C++ standard again:
"[Note:
both new int and new int [10] have type int* and the type of new
int[10] is
int (*)[10].]"


This says exactly what I have said , that an int* can point to an int or
an
array of int.


Paul, once again you are wrong of course. The Standard addresses this case
explicitly.

From the '03 C++ Standard 5.3.4 5th paragraph

"When the allocated object is an array (that is, the direct-new-declarator
syntax is used or the new-type-id or type-id denotes an array type), the
new-expression yields a pointer to the initial element (if any) of the
array.
[Note: both new int and new int[10] have type int* and the type of new
int[10] is int (*)[10]. ]"

Specifically: "yields a pointer to the initial element"


It doesn't say the pointer not a pointer to the array, which is what you are
saying . You are the one who is technically inaccurate and incorrect.
Any other implied meanings that you wish to add to this statement are
meaningful
in your head and your head only. The _growing_ list of competent C++
programmers who have replied to you seem to agree.

The only implied meanings that have been added are by you and a few other
idiots who imply this means the pointer is not a pointer to the array.
I would reference Öö Tiib's responses in particular. His logic also makes
distinctions that you fail to understand.

You can reference whatever the **** you like , its obvious to anyone , with
a brian , that reads this who fails to understand.
It should be noted that "yields a pointer to the initial element" is the
identical language used in TCPPPL and Accelerated C++.

It should be noted that you don't know what the **** you are talking about ,
you are a complete arsehole and an idiot who has no place in any respected
C++ community.
<snipped the rest which is predicated on Paul's feelings as to how the
Standard
should be worded versus what the Standard actually says>

No you are one suggesting the pointer does not point to an array.

I shall be deleting your posts without reading them from now on.

Plonk.
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
news:[email protected]... [snip]

Yes, it does. Here is the relevant quote from the C++ standard again:
"[Note:
both new int and new int [10] have type int* and the type of new
int[10] is
int (*)[10].]"


This says exactly what I have said , that an int* can point to an int or
an
array of int.
And also it confirms that, when dereferenced, a pointer to an array
returns
a pointed to (n-1)dimensional array.


The quote says nothing about whether an int* can point to an int or an
array
of int. It says

- new int returns an int*
- new int[10] returns an int*
- new int[10] returns an int (*)[10]

The quote says nothing about the result of dereferencing an array.


It clearly shows that a pointer to a 1dim array of int, is the same type as
a pointer to a single int.
The expression "new int[10]"
returns a pointer to an array.

This expression returns a pointer to a 2dimensional array.


According to the standard, "new int[10]" returns a "int (*)[10]",
a pointer to a 1-dimensional array. An example of a pointer to a
2-dimensional array is "int (*)[1][2]". Note the difference in the
number of square brackets.



No the standard doesn't say that a pointer of type int (*)[size] points to a
1dim array. Quite the opposite.

Yes you did.

Not when it's a 1dimensional array.

A pointer to an int (int*) and a pointer to a 1-dimensional array
(int (*)[]) are different types of pointers.
No you are confused by a pointer to object of array-type and a pointer to an
actuall array.
A pointer of type int (*)[size] is a pointer to a 2dim array, it also points
to an object of type int[size].


See the post entitled , "you snipped something" for clarification of how the
C++ standard defines this.

<snip>
..
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
[snip]

The quote says nothing about whether an int* can point to an int or an
array
of int. It says

- new int returns an int*
- new int[10] returns an int*
- new int[10] returns an int (*)[10]

The quote says nothing about the result of dereferencing an array.


It clearly shows that a pointer to a 1dim array of int, is the same type
as
a pointer to a single int.


If the pointer to a 1-dimensional array of were the same type as a pointer
to a single int, then given the declaration

int *i, ai[4];

Then the following assignment of a pointer to a 1-dimensional array of int
to a pointer to an int would be allowed

i = &ai;

No this would yield a pointer type of a different level of indirection.
but it is not allowed.

When you take the address of something you introduce another level of
indirection.
int x =5;
int* px = &x;
int** ppx = &px;
int*** pppx = &ppx;

If you try to take the address of an arrayobject you get a pointer type
which is different in levels of indirection.
int arr[6][6]; /*This is an array-object*/
int (*parr)[6] = arr; /*This is a pointer to THE array*/
int (*pparr)[6][6] = &arr; /*This is a pointer to an array-object*/

std::cout<< arr << std::endl << parr << std::endl << pparr;
All point to the same thing.
Each identifier points to the same array, but they are different levels of
indirection.
The same applies to 1dim arrays:
int arr[6]; /*array object*/
int* parr= arr; /*pointer to THE array*/
int (*pparr) =&arr; /*pointer to array object*/

This C++ standard confirms, in section 8.3.4 para 7, that a dereferenced
array object is always (n-1) dimensions of the pointed to array.
If you don't accpet what's in the standards I don't know what more I can
say.

<snip>
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
If the pointer to a 1-dimensional array of were the same type as a
pointer
to a single int, then given the declaration

int *i, ai[4];

Then the following assignment of a pointer to a 1-dimensional array of
int
to a pointer to an int would be allowed

i = &ai;
No this would yield a pointer type of a different level of indirection.

Applying the unary & operator results in a pointer to its operand. In the
example the operand (ai) is a 1-dimensional array, making the result of
applying the & operator a pointer to a 1-dimensional array.
Exactly what I said , you are making a pointer to the array object, its
value is no different. It points to exactly the same place.
Right, taking the address of an array gives a pointer to the array.

Yes but you don't seem to accept what I was explaining, that is..It
introduces another level of indirection.
int arr[6][6]; /*This is an array-object*/
int (*parr)[6] = arr; /*This is a pointer to THE array*/

It is a pointer to the first element of an array of 6 int[6] elements.
Its a pointer to the array, It can point to any part of the array becuase
its an l-value.
int (*pparr)[6][6] = &arr; /*This is a pointer to an array-object*/

It is a pointer to a 2-dimensional (6 by 6) array of int.
This is a different level of indirection. If I dereference this I get an
array-object , not the actual array data.
..
Right, the addresses are all the same value, but each pointer is a
different type.
They all point to arrays of int-type. They are different levels of
indirection.
If you increment parr, the byte address increases by
6*sizeof(int). If you increment pparr, the byte address increases by
6*6*sizeof(int).


Each identifier points to the same address, but parr is a pointer to a
1-dimensional array and pparr is a pointer to a 2-dimensional array.
No its not par is a pointer to THE array (the data that is the array), pparr
is not:
par[0][0]; /*returns array data*/
pparr[0][0] /*Is a complete pile of bollocks*/
The same applies to 1dim arrays:
int arr[6]; /*array object*/
int* parr= arr; /*pointer to THE array*/

It points to the first element of the array. When the initialization
expression is evaluated, the standard array-to-pointer conversion occurs,
resulting in a pointer to the first element of the array, not a pointer
to the array.

What don't you understand about the fact that they all initially point to
the same address?
Why you seem to think that parr is some kind of r-value is beyond me, what
don't you understand about the fact that it can point to any element of the
array?
It is a pointer to a 1-dimensional array of 6 ints.


I accept what is written in the C++ standard. In an earlier followup I
gave an example where that paragraph applied. I also accept what is
written in paragraph 1 of section 4.2:
No you don't accept it , you said it wasn't relevant.
An lvalue or rvalue of type "array of N 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.

An example where the paragraph applies is the initialization expression
in the second of the following two declarations.

int arr[6];
int *parr = arr;

According to the standard, the result of the conversion is a "pointer to
the
first element of the array" not a "pointer to the array".

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.*/

You obviously have very strange ideas about pointers and arrays, I have
already posted information about array types from experts and the C++
standard, if you refuse to accept this thats up to you. Please stop making
statements as if they are statements of factual correctness that are
actually wrong, particularly the one I quote:
<quote>
According to the standard, the result of the conversion is a "pointer to the
first element of the array" not a "pointer to the array".
</quote>
This is a complete misinterpretation of the standards, and incorrect..
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
int arr[6][6]; /*This is an array-object*/
int (*parr)[6] = arr; /*This is a pointer to THE array*/
int (*pparr)[6][6] = &arr; /*This is a pointer to an array-object*/ [snip]
Each identifier points to the same address, but parr is a pointer to a
1-dimensional array and pparr is a pointer to a 2-dimensional array.
No its not par is a pointer to THE array (the data that is the array),
pparr
is not:
par[0][0]; /*returns array data*/
pparr[0][0] /*Is a complete pile of bollocks*/

I wrote: "pparr is a pointer to a 2-dimensional array". To get the
same value that arr[0][0] gives, use the expression (*pparr)[0][0]. That
is, dereference the pointer to the array before applying subscript
operators.
The same applies to 1dim arrays:
int arr[6]; /*array object*/
int* parr= arr; /*pointer to THE array*/

It points to the first element of the array. When the initialization
expression is evaluated, the standard array-to-pointer conversion
occurs,
resulting in a pointer to the first element of the array, not a pointer
to the array.

What don't you understand about the fact that they all initially point to
the same address?

I never stated that they didn't point to the same address. What I wrote
was about the type of the expression arr. Your comment with the
declaration
was "pointer to THE array". I wrote that it was a pointer to the first
element of the array, not a pointer to the array. In the example, the
former is of type int* and the latter is of type int(*)[6].
Why you seem to think that parr is some kind of r-value is beyond me,
what
don't you understand about the fact that it can point to any element of
the
array?

I never stated that the value of parr is some king of rvalue. It is a
non-const identifier. Its value can be changed to point to any int.
You are a liar you have been constantly stating it doesn't point to the
array and it only points to only the first element.


[snip]
No you don't accept it , you said it wasn't relevant.

I wrote in a previous followup (message
section 8.3.4 wasn't relevant to the declaration

int *p = new int[4]

because the declaration did not dereference an array. I accept the
quote I posted from section 4.2 that you silently snipped.

[snip]

As I said you are trying to say the standards are not relevant when they
are.
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.


So why do you not understand the parr points to the array?


Incrementing parr adds sizeof(int) to the byte address. Incrementing
pparr
adds 6*sizeof(int) to the byte address


The ideas I have about pointers and arrays are from the standard. Here
is paragraph 1 of section 4.2 of the standard (you silently snipped
the copy that was in my previous post):

An lvalue or rvalue of type "array of N 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.

A pointer to the first element of the array is not the same as a pointer
to
the array.
WHere does the standard state this? Or it this your addition to the
standard?

If it points to the first element *OF THE ARRAY* then, by definition , it
points to the array.

<snip>
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
On 2011-03-28, Paul <[email protected]> wrote:
[snip]

You are a liar you have been constantly stating it doesn't point to the
array and it only points to only the first element.

What I have been stating is that it, that is, an rvalue or lvalue of
type array of T that undergoes array-to-pointer conversion, results in
a pointer to T that points to the first element of the array. If that
is a lie, then section 4.2 of the C++ specification contains a lie.
You are saying it points to the initial element of an array, yet it doesn't
point to the array.
Which does not make sense.

It does not point the array because a pointer to an array is a
different type of pointer than a pointer to an element of the array.
(More on this difference later in this followup.)

See there you go again ^^^^^^.

This C++ standard confirms, in section 8.3.4 para 7, that a
dereferenced
array object is always (n-1) dimensions of the pointed to array.
If you don't accpet what's in the standards I don't know what more I
can
say.

I accept what is written in the C++ standard. In an earlier followup
I
gave an example where that paragraph applied. I also accept what is
written in paragraph 1 of section 4.2:
No you don't accept it , you said it wasn't relevant.

I wrote in a previous followup (message
about
section 8.3.4 wasn't relevant to the declaration

int *p = new int[4]

because the declaration did not dereference an array. I accept the
quote I posted from section 4.2 that you silently snipped.

[snip]

As I said you are trying to say the standards are not relevant when they
are.

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.
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.
So why do you not understand the parr points to the array?

parr is a pointer to an int; it has type int*. A pointer to an array of
int has of the form int(*)[].
No you are wrong , this is A pointer to an array , this is not THE ONLY type
that points to an array.
parr does not point to the array because
its type is not that of a pointer to an array.

They all point to the same array. How can you not understand that, do you
have learning difficulties?

I demonstrated why they are not the same in lines that you sliently
snipped at this point. Here are the snipped lines:
You have demonstrated nothing.

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.
The C++ standard allows the right operand of an assignment operator
to be implicitly converted to the type of the left operand. However,
converting from a pointer to an array of N T to a pointer to T is
not a conversion allowed by section 4 of the C++ standard, where
the implicit conversions are specified.
You obviously dont know anything about the C++ standard if you cant
understand the simple statement :
points to the first element of an array, means 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.
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.
 
P

Paul

A. Bolmarcich said:
This C++ standard confirms, in section 8.3.4 para 7, that a
dereferenced
array object is always (n-1) dimensions of the pointed to array.
If you don't accpet what's in the standards I don't know what more
I
can
say.

I accept what is written in the C++ standard. In an earlier
followup
I
gave an example where that paragraph applied. I also accept what is
written in paragraph 1 of section 4.2:
No you don't accept it , you said it wasn't relevant.

I wrote in a previous followup (message
about
section 8.3.4 wasn't relevant to the declaration

int *p = new int[4]

because the declaration did not dereference an array. I accept the
quote I posted from section 4.2 that you silently snipped.

[snip]

As I said you are trying to say the standards are not relevant when
they
are.

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.
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.
So why do you not understand the parr points to the array?

parr is a pointer to an int; it has type int*. A pointer to an array of
int has of the form int(*)[].
No you are wrong , this is A pointer to an array , this is not THE ONLY
type
that points to an array.
parr does not point to the array because
its type is not that of a pointer to an array.

They all point to the same array. How can you not understand that, do you
have learning difficulties?

I don't have any learning difficulties. However, I do understand that
in C++ a pointer to an int points to an int; it does not point to an
array. A pointer to an int and an pointer to an array of int are
different types.
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.
A pointer to an int may point to any int, including one that is an
element of an array of int. However, when pointing to an element of
an array of int it is pointing to an int, not an array. When you
dereference the pointer the result is an int, not an array.
This is where you lose all sense of logic, a pointer to an entity points
exactly to that entity. The pointer type does not define the pointed to
entity.
[snip]
You have demonstrated nothing.

It appears that you think I have demonstrated nothing even before
reading the sliently snipped lines that were restored. That is
about as useful as write-only memory.
What exactly does SILENTLY snipped mean? Does snipping produce sound on your
PC?
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.
I know enough about the C++ standard to have quoted or referred to
parts of it that are relevant to C++ statements discussed in this
thread of posts.
You don't accept what is relevant , the part of the C++ standards that
defines arrays are relevant but you seem to think not.
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.
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?

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?
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.
 
P

Paul

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.
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
On 2011-03-31, Paul <[email protected]> wrote:
[snip]

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.

Actually, arr1 is a 2-dimensional array of int, p1 is a pointer to
int, new int[5] returns a pointer to an int, p2 is a pointer to an
array of int, and new int[5][6] returns a pointer to a
1-dimensional array of int. p1 and p2 are pointers, not arrays.

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.

Here is a note from paragraph 5 of section 5.3.4 (New) of the C++
standard: "Note: both new int and new int[10] have type int* and
the type of new int[10] is int (*)[10]."

[snip]
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.

If a pointer to an int and a pointer to an array of int were the
same type, then the statement

int (*pai)[1], *pa = pai;

would be allowed, but it isn't. A compiler I use gives the message
"error: cannot convert 'int (*)[1]' to 'int*' in initialization".
An attempt is being made to convert a pointer to an array of int to
a pointer to an int.
I know what points to means I dont need to reference the standard to find
out.

You need to reference the C++ standard to understand what a term
means in the C++ programming language.
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.

A section of the C++ standard about dereferencing a pointer is
relevant to a statement in which a pointer is dereferenced. The
same section is not relevant to a statement in which a pointer is
not dereferenced.
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.

That's right, the quote from the standard is about the unary *
operator It is not relevant to whether an int* can point to
an array. A pointer to an array points to an array of the number
of dimensions in the pointer type, not to an array of one fewer
dimensions. For example,

int (*p2i)[3][5]

declares a pointer to a 2-dimensional array, not to a 1-dimensional
array.
And I though you said the standards were irellevant because we were not
talking aobut dereferencing a pointer.

I said that a part of the standard about dereferencing is not
relevant to a statement, such as

int *p = new int[4];

in which there is no dereferencing. A part of the standard about
dereferencing is relevant to a statement in which there is a
dereference operation.
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;

It is good to see that you now understand that trying assign a
pointer to an array of int to a pointer to an int is wrong. When
the array name in the initialization expression is converted to is
a pointer to an int, not a pointer to an array of int.
You don't take the address of an array , unless you want a different
level
of indirection. Arrays are implicitly converted to pointers.

You take the address of an array when you want a pointer to that array.
It is allowed but it needs a cast.

Using a cast changes the type of a value. If the cast is to
something other than a pointer to an array of int, the value on
the left hand side of the assignment is no longer from a
pointer to an array of int.
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.

There is nothing bad about the statement, it assigns a pointer to
the first element of an array of int to a pointer to an int.
Evaluating it starts with pai, a pointer to an array; (*pai) is
that array; (*pai)[0] is the first element of that array;
&((*pai)[0]) is a pointer to the element; pi = &((*pai)[0]) sets
pi to that pointer.
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.

As far as I can tell my understanding is correct. pi is a pointer
an int and pai is a pointer to an array of int. If a pointer to an
int could point to a pointer to an array of int, the statement would
be allowed.
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?

It does not suggest that Bjarne Stroustrup is wrong, unless he
said that assigning a pointer to array of int to a pointer to int
is allowed in C++. As far as I know, he has never said that.
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.

According to the standard, a2i in the initialization expression is
converted to a pointer to a 1-dimension array, not a 2-dimensional
array. Paragraph 7 of section 8.3.4 of the standard is:

A consistent rule is followed for multidimensional arrays. If E
is an n-dimensional array of rank ixjx...xk, then E appearing in
an expression is converted to a pointer to an (n - 1)-dimensional
array with rank jx...xk. If the * operator, either explicitly or
implicitly as a result of subscripting, is applied to this pointer,
the result is the pointed to (n - 1)-dimensional array, which
itself is immediately converted into a pointer.
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, which is implicitly converted to a pointer
to the first element of that 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*/

The only difference between the initialization of p and p_x are
their types. Due to the initializatons, the expression (*p)
is equivalent to arr; the expression *p_x is equivalent to x.
All those expressions evaluate to an int.

Taking the address of something of type T results in a pointer to
type T. Dereferencing that pointer results in the thing whose
address was taken.

With pp there is another level of indirection from an int. The
expression *pp evaluates to a pointer to an int. The expression
**pp evaluates to an int. p_x is an additional object between
pp and the int that is the result of evaluating x. There is no
corresponding additional object with p.

Just because the name of an array when used in an expression
evaluates to a pointer to the first element of the array does not
mean that an array is a pointer. An external declaration of *arr
in one file does not correctly refer to an external definition of
arr[] in another file.

[snip]
I have answered any questins you have asked.

You have not answered the question: 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? What paragraph of what section?


I've already explained how it works if you don't understand then thats your
issue.
GL. I can't be bothered arguing abvout anymore especially when you refuse to
accept the relevent quotes from standards
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
On 2011-03-31, Paul <[email protected]> wrote:
[snip]

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.

Actually, arr1 is a 2-dimensional array of int, p1 is a pointer to
int, new int[5] returns a pointer to an int, p2 is a pointer to an
array of int, and new int[5][6] returns a pointer to a
1-dimensional array of int. p1 and p2 are pointers, not arrays.

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.

Here is a note from paragraph 5 of section 5.3.4 (New) of the C++
standard: "Note: both new int and new int[10] have type int* and
the type of new int[10] is int (*)[10]."

[snip]
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.

If a pointer to an int and a pointer to an array of int were the
same type, then the statement

int (*pai)[1], *pa = pai;

would be allowed, but it isn't. A compiler I use gives the message
"error: cannot convert 'int (*)[1]' to 'int*' in initialization".
An attempt is being made to convert a pointer to an array of int to
a pointer to an int.
I know what points to means I dont need to reference the standard to find
out.

You need to reference the C++ standard to understand what a term
means in the C++ programming language.
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.

A section of the C++ standard about dereferencing a pointer is
relevant to a statement in which a pointer is dereferenced. The
same section is not relevant to a statement in which a pointer is
not dereferenced.
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.

That's right, the quote from the standard is about the unary *
operator It is not relevant to whether an int* can point to
an array. A pointer to an array points to an array of the number
of dimensions in the pointer type, not to an array of one fewer
dimensions. For example,

int (*p2i)[3][5]

declares a pointer to a 2-dimensional array, not to a 1-dimensional
array.
And I though you said the standards were irellevant because we were not
talking aobut dereferencing a pointer.

I said that a part of the standard about dereferencing is not
relevant to a statement, such as

int *p = new int[4];

in which there is no dereferencing. A part of the standard about
dereferencing is relevant to a statement in which there is a
dereference operation.
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;

It is good to see that you now understand that trying assign a
pointer to an array of int to a pointer to an int is wrong. When
the array name in the initialization expression is converted to is
a pointer to an int, not a pointer to an array of int.
You don't take the address of an array , unless you want a different
level
of indirection. Arrays are implicitly converted to pointers.

You take the address of an array when you want a pointer to that array.
It is allowed but it needs a cast.

Using a cast changes the type of a value. If the cast is to
something other than a pointer to an array of int, the value on
the left hand side of the assignment is no longer from a
pointer to an array of int.
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.

There is nothing bad about the statement, it assigns a pointer to
the first element of an array of int to a pointer to an int.
Evaluating it starts with pai, a pointer to an array; (*pai) is
that array; (*pai)[0] is the first element of that array;
&((*pai)[0]) is a pointer to the element; pi = &((*pai)[0]) sets
pi to that pointer.
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.

As far as I can tell my understanding is correct. pi is a pointer
an int and pai is a pointer to an array of int. If a pointer to an
int could point to a pointer to an array of int, the statement would
be allowed.
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?

It does not suggest that Bjarne Stroustrup is wrong, unless he
said that assigning a pointer to array of int to a pointer to int
is allowed in C++. As far as I know, he has never said that.
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.

According to the standard, a2i in the initialization expression is
converted to a pointer to a 1-dimension array, not a 2-dimensional
array. Paragraph 7 of section 8.3.4 of the standard is:

A consistent rule is followed for multidimensional arrays. If E
is an n-dimensional array of rank ixjx...xk, then E appearing in
an expression is converted to a pointer to an (n - 1)-dimensional
array with rank jx...xk. If the * operator, either explicitly or
implicitly as a result of subscripting, is applied to this pointer,
the result is the pointed to (n - 1)-dimensional array, which
itself is immediately converted into a pointer.
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)

<snip>


<snip>
 
G

Garrett Hartshaw

A. Bolmarcich said:
[snip]

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.

Actually, arr1 is a 2-dimensional array of int, p1 is a pointer to
int, new int[5] returns a pointer to an int, p2 is a pointer to an
array of int, and new int[5][6] returns a pointer to a
1-dimensional array of int. p1 and p2 are pointers, not arrays.

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.

Here is a note from paragraph 5 of section 5.3.4 (New) of the C++
standard: "Note: both new int and new int[10] have type int* and
the type of new int[10] is int (*)[10]."

[snip]
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.

If a pointer to an int and a pointer to an array of int were the
same type, then the statement

int (*pai)[1], *pa = pai;

would be allowed, but it isn't. A compiler I use gives the message
"error: cannot convert 'int (*)[1]' to 'int*' in initialization".
An attempt is being made to convert a pointer to an array of int to
a pointer to an int.
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.

You need to reference the C++ standard to understand what a term
means in the C++ programming language.
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.

A section of the C++ standard about dereferencing a pointer is
relevant to a statement in which a pointer is dereferenced. The
same section is not relevant to a statement in which a pointer is
not dereferenced.
A pointer to an int may point to any int, including one that is an
element of an array of int. However, when pointing to an element of
an array of int it is pointing to an int, not an array. When you
dereference the pointer the result is an int, not an array.
This is where you lose all sense of logic, a pointer to an entity
points
exactly to that entity. The pointer type does not define the pointed to
entity.

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.

That's right, the quote from the standard is about the unary *
operator It is not relevant to whether an int* can point to
an array. A pointer to an array points to an array of the number
of dimensions in the pointer type, not to an array of one fewer
dimensions. For example,

int (*p2i)[3][5]

declares a pointer to a 2-dimensional array, not to a 1-dimensional
array.
And I though you said the standards were irellevant because we were not
talking aobut dereferencing a pointer.

I said that a part of the standard about dereferencing is not
relevant to a statement, such as

int *p = new int[4];

in which there is no dereferencing. A part of the standard about
dereferencing is relevant to a statement in which there is a
dereference operation.
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;

It is good to see that you now understand that trying assign a
pointer to an array of int to a pointer to an int is wrong. When
the array name in the initialization expression is converted to is
a pointer to an int, not a pointer to an array of int.
You don't take the address of an array , unless you want a different
level
of indirection. Arrays are implicitly converted to pointers.

You take the address of an array when you want a pointer to that array.
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.

Using a cast changes the type of a value. If the cast is to
something other than a pointer to an array of int, the value on
the left hand side of the assignment is no longer from a
pointer to an array of int.
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.

There is nothing bad about the statement, it assigns a pointer to
the first element of an array of int to a pointer to an int.
Evaluating it starts with pai, a pointer to an array; (*pai) is
that array; (*pai)[0] is the first element of that array;
&((*pai)[0]) is a pointer to the element; pi = &((*pai)[0]) sets
pi to that pointer.
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.

As far as I can tell my understanding is correct. pi is a pointer
an int and pai is a pointer to an array of int. If a pointer to an
int could point to a pointer to an array of int, the statement would
be allowed.
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?

It does not suggest that Bjarne Stroustrup is wrong, unless he
said that assigning a pointer to array of int to a pointer to int
is allowed in C++. As far as I know, he has never said that.
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.

According to the standard, a2i in the initialization expression is
converted to a pointer to a 1-dimension array, not a 2-dimensional
array. Paragraph 7 of section 8.3.4 of the standard is:

A consistent rule is followed for multidimensional arrays. If E
is an n-dimensional array of rank ixjx...xk, then E appearing in
an expression is converted to a pointer to an (n - 1)-dimensional
array with rank jx...xk. If the * operator, either explicitly or
implicitly as a result of subscripting, is applied to this pointer,
the result is the pointed to (n - 1)-dimensional array, which
itself is immediately converted into a pointer.
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)




<snip>

int a2[3][5]; //a2 is a two dimensional array.
int (*pa1)[5] = a2; //When a2 is used in an expression, it is
converted to a pointer to a one dimensional array. (N = 2, 2 - 1 = 1)

int a4[3][4][5][7]; //a4 is a 4 dimensional array.
int (*pa3)[4][5][7] = a4; //When a4 is used in an expression, it is
converted to a pointer to a 3 dimensional array. (N = 4, 4 - 1 = 3)
 
P

Paul

Garrett Hartshaw said:
[snip]

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.

Actually, arr1 is a 2-dimensional array of int, p1 is a pointer to
int, new int[5] returns a pointer to an int, p2 is a pointer to an
array of int, and new int[5][6] returns a pointer to a
1-dimensional array of int. p1 and p2 are pointers, not arrays.

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.

Here is a note from paragraph 5 of section 5.3.4 (New) of the C++
standard: "Note: both new int and new int[10] have type int* and
the type of new int[10] is int (*)[10]."

[snip]
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.

If a pointer to an int and a pointer to an array of int were the
same type, then the statement

int (*pai)[1], *pa = pai;

would be allowed, but it isn't. A compiler I use gives the message
"error: cannot convert 'int (*)[1]' to 'int*' in initialization".
An attempt is being made to convert a pointer to an array of int to
a pointer to an int.

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.

You need to reference the C++ standard to understand what a term
means in the C++ programming language.

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.

A section of the C++ standard about dereferencing a pointer is
relevant to a statement in which a pointer is dereferenced. The
same section is not relevant to a statement in which a pointer is
not dereferenced.

A pointer to an int may point to any int, including one that is an
element of an array of int. However, when pointing to an element of
an array of int it is pointing to an int, not an array. When you
dereference the pointer the result is an int, not an array.
This is where you lose all sense of logic, a pointer to an entity
points
exactly to that entity. The pointer type does not define the pointed to
entity.

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.

That's right, the quote from the standard is about the unary *
operator It is not relevant to whether an int* can point to
an array. A pointer to an array points to an array of the number
of dimensions in the pointer type, not to an array of one fewer
dimensions. For example,

int (*p2i)[3][5]

declares a pointer to a 2-dimensional array, not to a 1-dimensional
array.

And I though you said the standards were irellevant because we were not
talking aobut dereferencing a pointer.

I said that a part of the standard about dereferencing is not
relevant to a statement, such as

int *p = new int[4];

in which there is no dereferencing. A part of the standard about
dereferencing is relevant to a statement in which there is a
dereference operation.

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;

It is good to see that you now understand that trying assign a
pointer to an array of int to a pointer to an int is wrong. When
the array name in the initialization expression is converted to is
a pointer to an int, not a pointer to an array of int.

You don't take the address of an array , unless you want a different
level
of indirection. Arrays are implicitly converted to pointers.

You take the address of an array when you want a pointer to that array.

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.

Using a cast changes the type of a value. If the cast is to
something other than a pointer to an array of int, the value on
the left hand side of the assignment is no longer from a
pointer to an array of int.

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.

There is nothing bad about the statement, it assigns a pointer to
the first element of an array of int to a pointer to an int.
Evaluating it starts with pai, a pointer to an array; (*pai) is
that array; (*pai)[0] is the first element of that array;
&((*pai)[0]) is a pointer to the element; pi = &((*pai)[0]) sets
pi to that pointer.

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.

As far as I can tell my understanding is correct. pi is a pointer
an int and pai is a pointer to an array of int. If a pointer to an
int could point to a pointer to an array of int, the statement would
be allowed.

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?

It does not suggest that Bjarne Stroustrup is wrong, unless he
said that assigning a pointer to array of int to a pointer to int
is allowed in C++. As far as I know, he has never said that.

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.

According to the standard, a2i in the initialization expression is
converted to a pointer to a 1-dimension array, not a 2-dimensional
array. Paragraph 7 of section 8.3.4 of the standard is:

A consistent rule is followed for multidimensional arrays. If E
is an n-dimensional array of rank ixjx...xk, then E appearing in
an expression is converted to a pointer to an (n - 1)-dimensional
array with rank jx...xk. If the * operator, either explicitly or
implicitly as a result of subscripting, is applied to this pointer,
the result is the pointed to (n - 1)-dimensional array, which
itself is immediately converted into a pointer.

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)




<snip>

int a2[3][5]; //a2 is a two dimensional array.
int (*pa1)[5] = a2; //When a2 is used in an expression, it is converted to
a pointer to a one dimensional array. (N = 2, 2 - 1 = 1)


The term "the ponted to (n-1) dimension array" means that n is the
dimensional size of the pointed to array.
In the above case the pointed to array is a 2d array.

If the users of this newsgroup cannot understand how C++ uses pointers to
arrays, thats their problem and I don't give a dam anymore. I'm fed up
arguing with idiots, arseholes and queers.
 
P

Paul

A. Bolmarcich said:
A. Bolmarcich said:
On 2011-04-04, Paul <[email protected]> wrote:
[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;

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].


The implicit coversion mentioned in the omitted part complies with
paragraph 1 of section 4.2 (Array-to-pointer conversion) of the
standard. That paragraph is

An lvalue or rvalue of type "array of N 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.

Because pa1i was declared to be a pointer to an array of int, the
pointer to the first element of the array that *pa1i evaluates to is
a pointer to int.

WTF are you talking about , the only thing ommited was the decalration of
pa1i , which is obvious initialised to point to a 2d array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top