pointer to array types

P

Paul

There seems to have been much confusion about pointer to array types in C++.
Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad and
very wrong.
A pointer to an array of 1 dimension is always of base type of the array,
for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;


A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].


It has to be this way because when we index and array with the subscript the
array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type, it is
not an array type.

So remember a pointer to an array is always n-1 dim less than the array it
points to.

Please refer to your ISO C++ standards for more clarification.
TY and HTH.

P.S. I can't believe so many *experts* don't understand these basics.
 
S

SG

There seems to have been much confusion about
pointer to array types in C++.

This is not the case (see below).
Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

This code fragment won't even compile and I'm sure that other experts
know exactly why this is. It seems, you got something wrong and/or
spotted a typo in someone else's writing. Since you have a pointer to
an _array_ and not a pointer to an _int_ ...

int* : type of pointer to int
int(*)[6] : type of pointer to array of int

....you need to dereference it first in order to store something into
the array:

(*p)[2] = 56;

just like it is the case with every other pointer:

int* q = ...;
*q = 1729;

p: pointer to array
*p: the array itself (lvalue)
q: pointer to int
*q: the int itself (lvalue)

Keep in mind that when everybody (except you) uses the words "pointer
to array" they attatch a different meaning to these words (terminology
issue). And the disagreements you are faced with are only about the
meanings we attatch to words, not about "how C++ works". You
apparently use the words "pointer to array" as a shortcut for "pointer
that points to an element of an array". But this is just *imprecise*
and people are trying to explain this to you.
The above seems to be the opinion of many of the *experts*

This does not make any sense. How can a code fragment be an opinion?
What opinion are you talking about? Very imprecise wording as we have
come to expect from you.
A pointer to an array of 1 dimension is always of base type of the
array, for example int*.
The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;

A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].
It has to be this way because when we index and array with the subscript the
array is converted to a pointer+index and then dereferenced.
[...]
So remember a pointer to an array is always n-1 dim less than
the array it points to.

The actual message you are trying to convey here is well-known. It's
just your bad choice of words that everybody disagrees with.
P.S. I can't believe so many *experts* don't understand these basics.

You don't need to believe that because it is not true. It's not that
these experts don't understand how things work. It's that they
disagree with your choice of words when it comes to describing how
things work. This has always been the case. Always.

SG
 
N

Nobody

There seems to have been much confusion about pointer to array types in C++.
Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;

The last line should be:

(*p)[2] = 56;
or:
p[0][2] = 56;
A pointer to an array of 1 dimension is always of base type of the array,
for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;

You don't seem to understand the difference between a pointer to an array
and a pointer to an element of an array. Either is valid.
A pointer of type int(*p)[6], is a pointer to a 2 dim array

No, it's a pointer to an array of 6 integers. The array to which it points
may or may not be an element of a larger array.
like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].

Yes it is. "arr" is an array of 3 elements, each of which is an array of 6
integers. In the above code, p points to the first element of arr, i.e. it
points to an array of 6 integers.
It has to be this way because when we index and array with the subscript the
array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type, it is
not an array type.

In an lvalue context, arr is int[6]. In an expression context, it is
converted to int*.
So remember a pointer to an array is always n-1 dim less than the array it
points to.

Er, no. Again, you don't appear to understand the difference between a
pointer to an array and a pointer to an array element.
 
R

Rolf Magnus

Paul said:
There seems to have been much confusion about pointer to array types in
C++. Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad
and very wrong.

I hope not. The last line in your example is an error. It would have to be:

(*p)[2] = 56;

But pointer to arrays are rarely used. One could use a typedef to simplify
it a bit:

typedef int array[6];
array arr;
array* p = &arr;
A pointer to an array of 1 dimension is always of base type of the array,
for example int*.

That's not a pointer to an array. It's a pointer to an int - a single int.
It might happen to be the first (or second or tenth) element of an array,
but that doesn't influence the pointer type. It's still a pointer to an int.
Often, people use sloppy wording and talk of a "pointer to an array" when
they actually mean a pointer to its first element.
The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;

Yes, it should be done with a pointer to int.
A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:

No, it's not.
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].

Yes it is.
It has to be this way because when we index and array with the subscript
the array is converted to a pointer+index and then dereferenced.

No, it's because you can use array notation on a pointer for convenience.
The normal way to dereference a pointer is like that:

int i;
int* p = &i;
*p = 5;

As you can see, no array involved. But since such a pointer is often used to
point to the beginning of an array (i.e. its first element), you would often
have to write something like that:

int arr[10];
int* p = arr;
*(p+5) = 50;

And so as a convenient alternative,

p[5] = 50;

is equivalent.
So remember a pointer to an array is always n-1 dim less than the array it
points to.

How about the following code:

int* p = new int;
*p = 3;
delete p;

Where do you see an array here? Do you suggest I have to use delete[]
instead of delete, because I'm deleting an array, since p is a pointer to
one?
Please refer to your ISO C++ standards for more clarification.

I did. Maybe you should too. A good example can be found in §8.1 "Type
names":

****************************************************************************
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.’’ ]
****************************************************************************
 
P

Paul

SG said:
There seems to have been much confusion about
pointer to array types in C++.

This is not the case (see below).
Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

This code fragment won't even compile and I'm sure that other experts
know exactly why this is. It seems, you got something wrong and/or
spotted a typo in someone else's writing. Since you have a pointer to
an _array_ and not a pointer to an _int_ ...

int* : type of pointer to int
int(*)[6] : type of pointer to array of int
As I said it is wrong and very bad code.
...you need to dereference it first in order to store something into
the array:

(*p)[2] = 56;
What you have here is a pointerto a pointerto an array, and this is why you
need to dereference it twice to access the data, let me explain:

int x[1]= {5};
int* p_x = x; /*create a pointerto the array*/
int** p_p_x = &p_x; /*a pointerto pointerto the array */
std::cout<<(*p_p_x)[0]; /*Dereferenced twice: explicitly and implicitly*/

You are confused by the following two terms:
a) pointer to array
b) pointer to array-type
just like it is the case with every other pointer:

int* q = ...;
*q = 1729;

p: pointer to array
*p: the array itself (lvalue)
q: pointer to int
*q: the int itself (lvalue)
I don't know what the above code is suppposed to be suggesting.
Keep in mind that when everybody (except you) uses the words "pointer
to array" they attatch a different meaning to these words (terminology
issue).
Your terminology is incorrect , you are confused between:
a) a pointerto an array
b)a pointerto array-type

A pointer, of type int(*)[5], stores exactly one memory address.
A pointer, of type int* , stores exactly one memory address.
They can both point to the same initial element of a 2dim array. The
difference is int* is a pointerto a single dimensional array, int(*)[5] is a
pointer to a 2dim array.

With a pointerto an array, the pointer-type is always n-1 dimensions less
than the array it points to.
<snip>
 
P

Paul

[snip]

Please ignore our resident troll Paul, except for the one part where
he is right:

You're the troll because my whole post is right, and you cannot prove
otherwise.
 
R

RaZiel

There seems to have been much confusion about pointer to array types in
C++. Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad
and very wrong.
A pointer to an array of 1 dimension is always of base type of the
array, for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;


A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].


It has to be this way because when we index and array with the subscript
the array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type, it
is not an array type.

So remember a pointer to an array is always n-1 dim less than the array
it points to.

Please refer to your ISO C++ standards for more clarification.
TY and HTH.

P.S. I can't believe so many *experts* don't understand these basics.

Why don't you just give up? Why waste your saturday morning on feeble
minds, as you see it? If you used your dedication and energy for
something fruitful, I'm sure you could achieve something. But here,
honestly, you achieve nothing.

- RaZ
 
P

Paul

Nobody said:
There seems to have been much confusion about pointer to array types in
C++.
Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;

The last line should be:

(*p)[2] = 56;
or:
p[0][2] = 56;
This is because a pointerto a 2dim array is used to access a 1dim array.
This is not how it should be , this whole concept is wrong and very bad
programming practise.

A pointer to an array of 1 dimension is always of base type of the array,
for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;

You don't seem to understand the difference between a pointer to an array
and a pointer to an element of an array. Either is valid.
You are confusing the two terms:
a) pointerto an array
b)pointerto an array-type.

It's not that I don't understand , it's that your terminology is inaccurate.

A pointer of type int(*p)[6], is a pointer to a 2 dim array

No, it's a pointer to an array of 6 integers. The array to which it points
may or may not be an element of a larger array.

This is a pointerto array-type int[6]. It doesn't point to 6 memory
locations, it's value has one memory address.
When this type of pointer is dereferenced it returns a pointer to int[6].

Clarification:

int arr[1][5]= {0};
int (*p)[5]= arr;
std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
std::cout<< "typeid of *p:\t" <<typeid((*p)).name();

This is a pointer to a 2dim array. It points to an *array-type* int[5], that
is what it yields when dereferenced..


From the C++ standards:
"If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing in
an expression is converted to a pointer to an (n - 1 )dimensional
array with rank j´ . . . ´k. 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, which itself is immediately converted into a
pointer."

like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].

Yes it is. "arr" is an array of 3 elements, each of which is an array of 6
integers. In the above code, p points to the first element of arr, i.e. it
points to an array of 6 integers.

It's not its a pointer to array-type int[6], see above. It points to exactly
one memory address.


<snip>
 
P

Paul

Rolf Magnus said:
Paul said:
There seems to have been much confusion about pointer to array types in
C++. Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad
and very wrong.

I hope not. The last line in your example is an error. It would have to
be:

(*p)[2] = 56;

But pointer to arrays are rarely used. One could use a typedef to simplify
it a bit:

typedef int array[6];
array arr;
array* p = &arr;
A pointer to an array of 1 dimension is always of base type of the array,
for example int*.

That's not a pointer to an array. It's a pointer to an int - a single int.

No it's a pointer to int-type, if the value of that pointer is the address
of an array it's a pointer to an array.

It might happen to be the first (or second or tenth) element of an array,
but that doesn't influence the pointer type. It's still a pointer to an
int.

You are clearly confused between:
a) pointer to int
b) pointer to int-type
A pointer to an array is not a pointer to int but it may well be a pointer
to int-type
Often, people use sloppy wording and talk of a "pointer to an array" when
they actually mean a pointer to its first element.
A pointer to an array is a pointer to an address within the bounds of the
array. It is not neccessarrilly the first element.
The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;

Yes, it should be done with a pointer to int.

More accurate terminology is ... it should be done with pointer to int-type.
A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:

No, it's not.
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].

Yes it is.

This is the major stumbling block where people get confused with
pointerto-array and pointerto-array-types.
I will explain again:

int arr[1][5]= {0};
int (*p)[5]= arr;
std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
std::cout<< "typeid of *p:\t" <<typeid((*p)).name();

This is a pointer to a 2dim array. It points to an *array-type* int[5], that
is what it yields when dereferenced..

From the C++ standards:
"If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing in
an expression is converted to a pointer to an (n - 1 )dimensional
array with rank j´ . . . ´k. 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, which itself is immediately converted into a
pointer."
It has to be this way because when we index and array with the subscript
the array is converted to a pointer+index and then dereferenced.

No, it's because you can use array notation on a pointer for convenience.
The normal way to dereference a pointer is like that:

int i;
int* p = &i;
*p = 5;

As you can see, no array involved. But since such a pointer is often used
to
point to the beginning of an array (i.e. its first element), you would
often
have to write something like that:

int arr[10];
int* p = arr;
*(p+5) = 50;

And so as a convenient alternative,

p[5] = 50;

is equivalent.
This shows that you are capable of understanding that the pointer to "arr"
above is indeed a pointer to int-type.


<snip>
 
P

Paul

RaZiel said:
There seems to have been much confusion about pointer to array types in
C++. Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad
and very wrong.
A pointer to an array of 1 dimension is always of base type of the
array, for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;


A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].


It has to be this way because when we index and array with the subscript
the array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type, it
is not an array type.

So remember a pointer to an array is always n-1 dim less than the array
it points to.

Please refer to your ISO C++ standards for more clarification.
TY and HTH.

P.S. I can't believe so many *experts* don't understand these basics.

Why don't you just give up? Why waste your saturday morning on feeble
minds, as you see it? If you used your dedication and energy for something
fruitful, I'm sure you could achieve something. But here, honestly, you
achieve nothing.
I guess I just like to prove who is correct and who isn't.
I'm sure some of them will eventually understand and they will remember in
future.
What better things is there to do while I wait for the football to start on
a Saturday? :)
 
R

RaZiel

RaZiel said:
There seems to have been much confusion about pointer to array types in
C++. Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad
and very wrong.
A pointer to an array of 1 dimension is always of base type of the
array, for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;


A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].


It has to be this way because when we index and array with the subscript
the array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type, it
is not an array type.

So remember a pointer to an array is always n-1 dim less than the array
it points to.

Please refer to your ISO C++ standards for more clarification.
TY and HTH.

P.S. I can't believe so many *experts* don't understand these basics.

Why don't you just give up? Why waste your saturday morning on feeble
minds, as you see it? If you used your dedication and energy for
something fruitful, I'm sure you could achieve something. But here,
honestly, you achieve nothing.
I guess I just like to prove who is correct and who isn't. Why?
I'm sure some of them will eventually understand and they will remember
in future.
Why? Does it matter to you if they don't understand? Will their
understanding affect you somehow? I'm sure they will remember you, but
for other reasons than you think.
What better things is there to do while I wait for the football to start
on a Saturday? :)
Sleep, walk, go out eating, go to the market/mall, have fun with your
family/friends, go to the local pub to watch the football match, etc..

Nevertheless, enjoy the match.

- RaZ
 
P

Paul

RaZiel said:
RaZiel said:
On 19.03.2011 06:13, Paul wrote:
There seems to have been much confusion about pointer to array types in
C++. Ahem many C++ experts seem to think the following is true:

/*Very wrong and very bad stuff here*/
int arr[6];
int (*p)[6]= &arr;
p[2] = 56;
/*Very wrong and very bad stuff here*/

The above seems to be the opinion of many of the *experts* in this
newsgroups of how to use pointers to arrays in C++ but this is very bad
and very wrong.
A pointer to an array of 1 dimension is always of base type of the
array, for example int*. The above code should be done like this:
int arr[6];
int* p= arr;
p[2] = 56;


A pointer of type int(*p)[6], is a pointer to a 2 dim array like so:
int arr[3][6];
int (*p)[6] = arr;
This is NOT a pointer to an array of int[6].


It has to be this way because when we index and array with the
subscript
the array is converted to a pointer+index and then dereferenced.
With:
int arr[6];
arr[2] = 0;
The value returned from the dereferenced pointer must be an int type,
it
is not an array type.

So remember a pointer to an array is always n-1 dim less than the array
it points to.

Please refer to your ISO C++ standards for more clarification.
TY and HTH.

P.S. I can't believe so many *experts* don't understand these basics.






Why don't you just give up? Why waste your saturday morning on feeble
minds, as you see it? If you used your dedication and energy for
something fruitful, I'm sure you could achieve something. But here,
honestly, you achieve nothing.
I guess I just like to prove who is correct and who isn't.
Why?
A matter of technical correctness.
Why? Does it matter to you if they don't understand? Will their
understanding affect you somehow? I'm sure they will remember you, but for
other reasons than you think.

They won't remeber me because they don't know me, they may remember some
posts in a newsgroup.
If they chose to think these posts originate from some kind of subhuman
entity who will always be incorrect,, that's their problem.
But this is just some misconception constructed in their minds.
Sleep, walk, go out eating, go to the market/mall, have fun with your
family/friends, go to the local pub to watch the football match, etc..

Nevertheless, enjoy the match.
TY , you have a nice day too whatever you chose to do . :)
 
R

Rolf Magnus

Paul said:
typedef int array[6];
array arr;
array* p = &arr;
A pointer to an array of 1 dimension is always of base type of the
array, for example int*.

That's not a pointer to an array. It's a pointer to an int - a single
int.

No it's a pointer to int-type, if the value of that pointer is the address
of an array it's a pointer to an array.

But you didn't use the address-of operator on an array to get it.
You are clearly confused between:

I'm not at all confused.
a) pointer to int
b) pointer to int-type

Which is the same thing.
A pointer to an array is not a pointer to int but it may well be a pointer
to int-type

So it does not point to an int, but to something that is of type int?
Strange wording.
This is the major stumbling block where people get confused with
pointerto-array and pointerto-array-types.
I will explain again:

int arr[1][5]= {0};

That's an array of arrays, i.e. a multi-dimensional array.
int (*p)[5]= arr;

You could also write that as:

int (*p)[5] = &arr[0];

i.e. the address of the element of arr that is at index 0.
std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
std::cout<< "typeid of *p:\t" <<typeid((*p)).name();

This is a pointer to a 2dim array.

You didn't use an address operator on a 2-dimensional array.
From the C++ standards:
"If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing in
an expression is converted to a pointer to an (n - 1 )dimensional
array with rank j´ . . . ´k. 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, which itself is
immediately converted into a pointer."

Right. Just as I said. The example that's following that paragraph clears
that up. I've marked the most important parts:

****************************************************************************
[Example: consider

int x[3][5];

Here x is a 3×5 array of integers. When x appears in an expression, it is
converted to a pointer to (the first of three) five-membered arrays of
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
integers.
In the expression x, which is equivalent to *(x+i), x is
first converted to a pointer as described; then x+i is converted to the type
of x, which involves multiplying i by the length of the object to which the
pointer points, namely five integer objects. The results are added
and indirection applied to yield an array (of five integers), which in turn
is converted to a pointer to the first of the integers. If there is another
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subscript the same argument applies again; this time the result is an inte-
ger. ]
****************************************************************************

Note the wording. Both times, you get a pointer to *the first element* of
the array. Not a "pointer to the array" as you claim.
 
P

Paul

Rolf Magnus said:
Paul said:
typedef int array[6];
array arr;
array* p = &arr;

A pointer to an array of 1 dimension is always of base type of the
array, for example int*.

That's not a pointer to an array. It's a pointer to an int - a single
int.

No it's a pointer to int-type, if the value of that pointer is the
address
of an array it's a pointer to an array.

But you didn't use the address-of operator on an array to get it.

You don't need to, it's explicitly converted to a pointer:
int arr[5];
int* p = arr;
std::cout<<"&arr:\t"<< &arr << "\np:\t"<< p;

I'm not at all confused.


Which is the same thing.
There you go , there is the source of your confusion.
It's not the same thing, if you have a pointer pointing to memory location
0F0000, the entity at this location is what the pointer points to.

In the following, the pointer "p" points to an array:
int arr[5];
int* p = arr; /*Pointer to an array*/

And next it points to an int:
int x;
p=&x; /*Pointer to an int*/

The same pointer type can point to different entities. The allocation
defines what entity the pointer points to, not the pointer-type.

Consider the following:

class A{
public:
virtual ~A(){std::cout<<"A destructor\n";}
};
class B:public A{~B(){std::cout<<"B destructor\n";}};
class C:public A{~C(){std::cout<<"C destructor\n";}};

int main()
{
A* p_a = new B;
delete p_a;
p_a = new C;
delete p_a;
std::cout<< "typeid p_a:\t"<< typeid(p_a).name();
}

In the above code p_a
a) is a pointer to A type
b) points to different types of objects

A pointer to object != pointer to object type.



So it does not point to an int, but to something that is of type int?
Strange wording.
No it's quite simple, it points to an array of ints. People are making it
confusing with erroneous statements like.. "it's not an array but just one
single integer".
This is the major stumbling block where people get confused with
pointerto-array and pointerto-array-types.
I will explain again:

int arr[1][5]= {0};

That's an array of arrays, i.e. a multi-dimensional array. Yes.
int (*p)[5]= arr;

You could also write that as:

int (*p)[5] = &arr[0];
Yes you could.
i.e. the address of the element of arr that is at index 0.
Elements need two levels of indirection i.e:
&arr[0][0].
std::cout<< "typeid of p:\t" <<typeid(p).name()<<std::endl;
std::cout<< "typeid of *p:\t" <<typeid((*p)).name();

This is a pointer to a 2dim array.

You didn't use an address operator on a 2-dimensional array.
correct.
From the C++ standards:
"If E is an ndimensional array of rank i´ j´ . . . ´k, then E appearing
in
an expression is converted to a pointer to an (n - 1 )dimensional
array with rank j´ . . . ´k. 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, which itself is
immediately converted into a pointer."

Right. Just as I said. The example that's following that paragraph clears
that up. I've marked the most important parts:

****************************************************************************
[Example: consider

int x[3][5];

Here x is a 3×5 array of integers. When x appears in an expression, it is
converted to a pointer to (the first of three) five-membered arrays of
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
integers.
In the expression x, which is equivalent to *(x+i), x is
first converted to a pointer as described; then x+i is converted to the
type
of x, which involves multiplying i by the length of the object to which
the
pointer points, namely five integer objects. The results are added
and indirection applied to yield an array (of five integers), which in
turn
is converted to a pointer to the first of the integers. If there is
another
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subscript the same argument applies again; this time the result is an
inte-
ger. ]
****************************************************************************

Note the wording. Both times, you get a pointer to *the first element* of
the array. Not a "pointer to the array" as you claim.

Where does it say "a pointer to the first element of an array is not a
pointer to the array" ? You must be reading something other than you've
posted.

Do you really think that if a pointer points to the initial element of an
array it no longer points to the array? :-S
The only we can point to the whole array simultaneously is to use an array
of pointers.

The quote I posted actually confirms what I said re:

"the result is the pointedto (n - 1 )dimensional array"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The term "the pointedto array" clarifies all.
 
F

Fred Zwarts

Paul said:
There you go , there is the source of your confusion.
It's not the same thing, if you have a pointer pointing to memory location
0F0000, the entity at this location is what the pointer points to.

Sorry, in C++ pointers don't point to types. Pointers point to objects.
So, a pointer to int-type is another language.

The fact that the pointer contains the address of an object, does not mean
that it is a pointer to that object.
Consider

union AllTypes {
int Integer;
int IntArray[5];
float Floating;
float FloatArray[18];

etc. etc.

}

AllTypes x;

int * IntPointer = &x.Integer;

Now IntPointer is a pointer to an int object.
IntPointer is not a pointer to a float object.
IntPointer is not a pointer to an float array.
etc. etc.
IntPointer is not a pointer to an int array.

Only, by redefining the meaning of "pointer to",
you can deny this.
 
P

Paul

No, pointers point to an entity, that entity is not necessarrilly an object.
A pointer-to int-type means exactly the same thing as pointer-to entity of
int-type.
The fact that the pointer contains the address of an object, does not mean
that it is a pointer to that object. You mean thats what you've been thinking about?
Consider

union AllTypes {
int Integer;
int IntArray[5];
float Floating;
float FloatArray[18];

etc. etc.

}

AllTypes x;

int * IntPointer = &x.Integer;

Now IntPointer is a pointer to an int object.
IntPointer is not a pointer to a float object.
IntPointer is not a pointer to an float array.
etc. etc.
IntPointer is not a pointer to an int array.

At the end of the day who cares what it's NOT a pointer to, what are you
trying to demonstrate?
 
F

Fred Zwarts

Paul said:
No, pointers point to an entity, that entity is not necessarrilly an
object.
A pointer-to int-type means exactly the same thing as pointer-to entity of
int-type.
The fact that the pointer contains the address of an object, does not
mean that it is a pointer to that object. You mean thats what you've been thinking about?
Consider

union AllTypes {
int Integer;
int IntArray[5];
float Floating;
float FloatArray[18];

etc. etc.

}

AllTypes x;

int * IntPointer = &x.Integer;

Now IntPointer is a pointer to an int object.
IntPointer is not a pointer to a float object.
IntPointer is not a pointer to an float array.
etc. etc.
IntPointer is not a pointer to an int array.

At the end of the day who cares what it's NOT a pointer to, what are you
trying to demonstrate?

Who cares? I thought you would care.
If not, finally, you don't care that IntPointer[2] can be used to access an
array element,
but that at the same time IntArray is not a pointer to an array.
 
P

Paul

No, pointers point to an entity, that entity is not necessarrilly an
object.
A pointer-to int-type means exactly the same thing as pointer-to entity of
int-type.
The fact that the pointer contains the address of an object, does not
mean that it is a pointer to that object. You mean thats what you've been thinking about?
Consider

union AllTypes {
int Integer;
int IntArray[5];
float Floating;
float FloatArray[18];

etc. etc.

}

AllTypes x;

int * IntPointer = &x.Integer;

Now IntPointer is a pointer to an int object.
IntPointer is not a pointer to a float object.
IntPointer is not a pointer to an float array.
etc. etc.
IntPointer is not a pointer to an int array.

At the end of the day who cares what it's NOT a pointer to, what are you
trying to demonstrate?

--Who cares? I thought you would care.
--If not, finally, you don't care that IntPointer[2] can be used to access
an
--array element,
--but that at the same time IntArray is not a pointer to an array.


I don't know what you are trying to state , sorry.
 
F

Fred Zwarts

Paul said:
It might happen to be the first (or second or tenth) element of an
array,
but that doesn't influence the pointer type. It's still a pointer to
an
int.

You are clearly confused between:

I'm not at all confused.

a) pointer to int
b) pointer to int-type

Which is the same thing.

There you go , there is the source of your confusion.
It's not the same thing, if you have a pointer pointing to memory
location 0F0000, the entity at this location is what the pointer points
to.

Sorry, in C++ pointers don't point to types. Pointers point to objects.
So, a pointer to int-type is another language.

No, pointers point to an entity, that entity is not necessarrilly an
object.
A pointer-to int-type means exactly the same thing as pointer-to entity
of int-type.
The fact that the pointer contains the address of an object, does not
mean that it is a pointer to that object.
You mean thats what you've been thinking about?
Consider

union AllTypes {
int Integer;
int IntArray[5];
float Floating;
float FloatArray[18];

etc. etc.

};

AllTypes x;

int * IntPointer = &x.Integer;

Now IntPointer is a pointer to an int object.
IntPointer is not a pointer to a float object.
IntPointer is not a pointer to an float array.
etc. etc.
IntPointer is not a pointer to an int array.

At the end of the day who cares what it's NOT a pointer to, what are you
trying to demonstrate?

--Who cares? I thought you would care.
--If not, finally, you don't care that IntPointer[2] can be used to access
an
--array element,
--but that at the same time IntArray is not a pointer to an array.


I don't know what you are trying to state , sorry.

OK, let me see if I can say it even more clearly.

IntPointer is a pointer to an object of type int.
IntPointer now points to the int x.Integer.
Because of the union, IntPointer[2] accesses an element of x.IntArray.
I said that IntPointer is not a pointer to the array x.IntArray.
You said: Who cares?
But earlier you said that if a pointer points to a memory location
that contains an array, it must be a pointer to an array.
So, I got the impression that you would care about this.

Because of the union
IntPointer also points to a memory location of a float
and to a memory location of a float array, etc. etc.
According to your reasoning,
IntPointer would be also a pointer to a float,
and to a float array, etc. etc.

After the assignment
x.Floating=3.14;
the memory location IntPointer points to, contains a float object.
Still, IntPointer is not a pointer to a float
and cannot be used like that without a cast.
So, the fact that the value of a pointer is an address
of a memory location that happens to contain an object of some type,
does not mean that the pointer is a pointer to such an object.
Saying that an int* is a pointer to a float is not the intention of the C++
language,
even if the address contained in it happens to be a location of a float.

So, may I conclude that an int* pointer that happens to contain the address
of an int array is not a pointer to an array according to the terminology of
C++?
Or do you still say: Who cares?
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top