Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer to array types

Reply
Thread Tools

pointer to array types

 
 
Paul
Guest
Posts: n/a
 
      03-19-2011
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.





 
Reply With Quote
 
 
 
 
Joshua Maurice
Guest
Posts: n/a
 
      03-19-2011
On Mar 18, 10:13*pm, "Paul" <(E-Mail Removed)> wrote:
[snip]

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

>Please refer to your ISO C++ standards for more clarification.


He refuses to use the same language of discourse as the rest of us,
and that frequently makes him not right, but not even wrong.

http://rationalwiki.org/wiki/Not_even_wrong
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      03-19-2011
On 19 Mrz., 06:13, Paul wrote:
> 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
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      03-19-2011
On Sat, 19 Mar 2011 05:13:36 +0000, 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;


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.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-19-2011
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.


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


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-19-2011

"SG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 19 Mrz., 06:13, Paul wrote:
>> 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>

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-19-2011

"Joshua Maurice" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Mar 18, 10:13 pm, "Paul" <(E-Mail Removed)> wrote:
[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.

 
Reply With Quote
 
RaZiel
Guest
Posts: n/a
 
      03-19-2011
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.

- RaZ
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-19-2011

"Nobody" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Sat, 19 Mar 2011 05:13:36 +0000, 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;

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

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-19-2011

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:im1ngc$1ah$01$(E-Mail Removed)-online.com...
> 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.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Casting from pointer types to non-pointer types jois.de.vivre@gmail.com C++ 11 06-07-2007 09:17 AM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments