Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #4584
| Newsgroups | comp.lang.c++ |
|---|---|
| From | "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> |
| Subject | Re: Problem with array objects |
| References | (1 earlier) <y%Gtp.39557$o81.27663@newsfe28.ams2> <slrnirgm0l.1f0l.aggedor@earl-grey.cloud9.net> <O41up.44235$5t2.13002@newsfe06.ams2> <slrnirluvl.11b6.aggedor@earl-grey.cloud9.net> <ZRDup.44374$0j7.31337@newsfe20.ams2> |
| Message-ID | <slrnirttg4.1nli.aggedor@earl-grey.cloud9.net> (permalink) |
| Date | 2011-05-02 13:20 -0500 |
On 2011-04-29, Paul <pchristor@yahoo.co.uk> wrote:
>
> "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> wrote in message
> news:slrnirluvl.11b6.aggedor@earl-grey.cloud9.net...
>> On 2011-04-27, Paul <pchristor@yahoo.co.uk> wrote:
>>>
>>> "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> wrote in message
>>> news:slrnirgm0l.1f0l.aggedor@earl-grey.cloud9.net...
>>>> On 2011-04-26, Paul <pchristor@yahoo.co.uk> wrote:
>>>>>
>>>>> "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> wrote in message
>>>>> news:slrnire0ks.gb4.aggedor@earl-grey.cloud9.net...
>> [snip]
>>>> What you wrote was
>>>>
>>>> No it doesn't
>>>> *arr =1; /*Accesses the array*/
>>>> *pparr=1; /*Cannot do this*/
>>>>
>>>> in reply to "Dereferencing pparr accesses an array in the same
>>>> sense that using the name arr accesses an array."
>>> This statement is incorrect, dereferencing pparr does not access the
>>> array
>>> in the same sense as arr.
>>
>> In the snipped part, arr and pparr were declared with
>>
>> int arr[40] = {0};
>> int (*pparr)[40] = &arr;
>>
>> An expression that dereferencing pparr has the same result as using
>> arr; arr and *pparr in expressions have the same type and value. An
>> example is (*pparr)[3] and arr[3]; they both are the element of arr
>> 3 after its first element.
>>
>> What is an example where dereferencing pparr does not have the
>> same result as arr?
> You said ..dereferencing pparr accesses the array in the same way as arr,
> which it doesn't.
You claim "it doesn't", but you do not provide an example that
supports your claim. I have provided examples that supports mine.
> You were/are not accepting the point I was making about dereferncing pparr.
> Which was that it does not point to the array directly, it points to an
> array-type object.
I don't accept a point about C++ that is not supported by the C++
standard. pparr points directly to the array; it does not point to
another object. Look at the output of the following program.
#include <iostream>
#include <typeinfo>
int main() {
int arr[40], (*pparr)[40]=&arr;
std::cout
<<"arr"
<<(arr==*pparr?"==":"!=")
<<"*pparr"
<<" "
<<"typeid(arr).name()"
<<(typeid(arr).name()==typeid(*pparr).name()?"==":"!=")
<<"typeid(*pparr).name()"
<<std::endl;
}
The output that I get is that the values and types of arr and
*pparr are the same.
>>>> Your example uses *arr and *pparr as if they were the same; they
>>>> are not the same. *arr refers to the first element of the array
>>>> while *pparr refers to the array. *pparr and arr both refer to
>>>> the array.
>>> So does :
>>> int* p = arr;
>>> So p accesses the array in the same sense as arr.
>>> pparr has another level of indirection.
>>
>> p does not access an array in the same sense as arr. p and arr are
>> different types: p is a pointer to int, while arr is an array of int.
> It doesn't matter if they are different types, they still access the array
> in the same sense, because arr is implicitly converted to the same type as
> p.
That implicit conversion is done only in some contexts. p does not
access the array in same sense as arr in other contexts. *pparr
accesses the array in the same sense as arr in all contexts.
>> An array and a pointer are not the same, although in some contexts,
>> such as that initialization, an array is implcitly converted to a
>> pointer to it first element.
> Nobody is saying an array and a pointer are the same. They access the array
> in the same the sense.i.e:
> arr[1];
> p[1];
> Both are subscripted only once to access the array.
Someone who says a pointer to the first element of an array is
pointing to an array is saying the pointer and the array the element
is in are the same.
>> pparr is a level of indirection from arr just as any pointer to
>> something in C++ is a level of indirection from what it points to.
> No its not with arrays, an array of integers is pointed to by an int*( in
> most cases), it can also be pointed to by an int(*)[N] for the purpose of
> converting to reference, for example.
I don't understand what your reply has to do what the statement I
made about a level of indirection.
>>>> Because pparr is a pointer to an array, of course it has to be
>>>> dereference twice to get the same result as deferencing the array
>>>> once. In general, if X points to Y, **X is the same as *Y.
>>>>
>>> When you have a pointer to an integer you don't derefernece it twice to
>>> access the integer.
>>> So why do you say "of course it has to be dereferneced twice"?
>>> A pointer to something should not have to be dereferenced twice to access
>>> what it points to.
>>
>> I said pparr is dereferenced twice has the same result as
>> dereferencing arr (the name of the object that pparr points to).
>> Dereferencing pparr once has the same result as using arr.
> Yes so does this not prove to you that pparr is a different level of
> indirection than arr?
> pparr is a pointer to an array-type object. This does not automatically
> imply that an int* cannot point to an array of integers.
As I have said, pparr is a level of indirection from arr, just at
any pointer to something is a level of indirection away from what
it points to. A pointer to an element of an array is a level of
indirection from an element of an array, not a level of indirection
from an array.
The fact that an int(*)[] (pointer to array of int) cannot be assigned
to an int* (pointer to int) implies that an int* cannot point to an
array of integers. An int* can point to an element of an array of
integers.
>>
>> With
>>
>> int i, *pi = &i;
>>
>> pi is a level of indirection from i. Dereferencing pi accesses an int
>> in the same sense that i does. Changing i to declare and array and pi
>> to declare a pointer to an array, changes the declaration to
>>
>> int arr[40], (*pparr)[40] = &arr;
>>
>> Given that declaration, dereferencing pparr accesses an array in the
>> same sense that arr does, just as prior to the change pi accesses an
>> int in the same sense that i does.
> Yes so this proves that pparr is a different level of indirection than arr.
> arr accesses the array by directly converting to a pointer , pparr needs
> indirection and then converted to a pointer.
The results of dereferencing pparr and using arr are the same (see
the output of the program given eariler in this followup). In
other words, dereferencing pparr accesses an array in the same sense
that using the name arr accesses an array.
[snip]
>> In C++ an object is a region of storage. The snipped declarations
>>
>> int arr[4] = {1,2,3,4};
>> int (*p)[4] = &arr;
>>
>> allocate an object named arr (and four unmaned subobjects in it) and
>> an object named p. Because of the initialzation, the expressions **p
>> and *arr have the same result. The expression **p access only one
>> stored memory address: the one stored in the object named p. There
>> is no other object between p and **p.
>>
> So answer the question if you are so smart.
> What object stores the value of the address we get when we dereference
> pparr?
The result of dereferencing pparr, such as in the subexpression *p of
**p == 1
is not stored in an object. It is not a matter of being smart; it
is a matter of understanding the definition of object in the C++
standard and certain details of C++ compilers.
[snip]
>>>> Evaluating
>>>> *pparr loads that value from memory. In a context where
>>>> array-to-pointer conversion is done, there is no additional C++
>>>> object whose value is obtained to convert from the address of an
>>>> array to a pointer to the first element of the array.
>>>>
>>> I don't think so , *pparr dereferences a pointer, there is no
>>> array-to-pointer conversion here.
>>
>> pparr is a pointer to an array. The result of *pparr is an array.
>> Array-to-pointer conversion is implicitly done on that result.
>
> That would be pointer-array conversion , not array-pointer.
The section in the C++ standard has the heading "Array-to-pointer
conversion". I use the same term as is used in the C++ standard.
[snip]
> A pointer to type T can point to a whole array of T's.
> Maybe it cannot access them all at the same time but then , no pointer type
> can.
A pointer to type T can point to an element of an array of T. At
any one time, it points to at most one element of an array of T. At
no time does it point to a whole array a T's. A whole array of T's
is pointed to by a pointer to an array of T, not by a pointer to T.
>>>> A pointer to an array of int is
>>>> type int(*)[N], where N is the number of array elements, if known.
>>>> A pointer to an array of int is dereferenced once to access the array.
>>> No this is a poointer to array-type objects, not to integer type objects.
>>
>> What do you mean "No"? I wrote that it is a "pointer to an array of
>> int", not a pointer to an int.
> But you omit the word TYPE.
> A pointer to an array of integer objects, or a single int, is of type int*.
> A pointer to an array of int-array-objects, or a single int-array-object ,
> is of type int(*)[N]
In what I wrote, replacing "pointer to an" with "pointer of type"
results in: 'a "pointer of type array of int", not a pointer of
type int'. A pointer of type array of int points to an object with
an array type, not to an object with an int type.
[snip]
>> The result of dereferencing it once is an array, just as the name of
>> the array in an expression is.
> dereferencing it yields an array-type object, you know the one that stores
> the memory address, not THE array of integer objects.
Dereferencing it (a pointer to an array of int) results in an array
of int, the one that was allocated by the declaration of the array
whose address was assgined to the pointer to array. Dereferencing
a pointer to an array of int does not result in an object that
stores the memory address of an array.
[snip]
> In C++ a pointer-type does not define what is pointed to.
In C++ the pointer-type determines the type of the result of
dereferencing the pointer. Here is paragraph 1 of section 5.3.1 (Unary
operators) of the C++ standard:
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.]
I accept what is in the C++ standard: "If the type of the expression
is pointer to T, the type of the result is T."
>>>>> A pointer to , for example a char array, is generally consider to be of
>>>>> type
>>>>> char*. An array by nature can only be accesssed one element at a time.
>>>>
>>>> What something is generally considered to be is not necessarily what
>>>> it is according to the C++ standard. According to the C++ standard
>>>> char(*)[N] is a pointer to an array of char, while char* is a pointer
>>>> to a char.
>>> No a char(*)[N] is a TYPE pointer to an array.
>>> Its all about types. An array of objects of type T, is pointed to by a
>>> T*.
>>> A T(*)[] type pointer can also point to the array but indirectly.
>>
>> That's right, it is about types. A T* points to a T, not an array of
>> T. A (*T)[] points to an array of T. A pointer is a level of
>> indirection from what it points to.
> its different with arrays.
No, pointers and levels of indirection are the same with arrays.
What is different with arrays is that in some contexts an array is
implicitly converted to a pointer to the first element of the array.
>>>> In some contexts a char* is treated like a pointer to an element of
>>>> an array; in some contexts an array is treated like a pointer to its
>>>> first element. Because in some contexts a thing is treated as if
>>>> it were something else, does not the thing is the something else.
>>>
>>> But you have things back to front , a pointer to an array of chars is
>>> firstmost a char*, a char(*)[N] is a pointer to an array-type.
>>
>> I have things exactly how they are in the C++ standard, specifically
>> as in the following example from section 8.1 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.
>>
>> According to the C++ standard
>>
>> int *pi
>>
>> declares a "pointer to int" and
>>
>> int (*p3i)[3]
>>
>> declares a "pointer to array of 3 int". pi may point to an element
>> in an array of int, but pointing to an element of an array is not
>> the same as pointing to an array, as p3i does.
> No the standard say its of TYPE pointer to array.
You are the one who wrote (quoted about 50 lines above): "Its all
about types." A value of type int* points to an int and a value of
type int(*)[] points to an array of int; the two are not the same.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-20 17:29 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-04-23 12:59 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-23 20:31 +0100
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-24 09:26 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-24 00:04 +0100
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-24 11:24 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-24 02:16 +0100
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-24 14:10 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-24 04:37 +0100
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-24 16:11 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-24 13:31 +0100
Re: Problem with array objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-24 17:57 +0200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-24 17:39 +0100
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-25 10:19 +1200
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-25 10:12 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-25 00:46 +0100
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-25 11:59 +1200
Re: Problem with array objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-25 05:01 +0200
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-04-25 14:56 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-25 10:57 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-04-26 12:36 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-26 22:42 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-04-27 12:53 -0500
Re: Problem with array objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-27 19:06 +0100
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-27 20:09 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-04-29 12:57 -0500
Re: Problem with array objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-29 19:10 +0100
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-04-29 19:56 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-02 13:20 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-02 22:43 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-05 14:13 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-05 22:21 +0100
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-05-06 10:49 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-06 12:29 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-07 11:34 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-08 04:05 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-10 12:31 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-10 22:10 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-12 12:19 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-13 09:54 +0100
Re: Problem with array objects "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-14 11:20 -0500
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-14 23:59 +0100
Re: Problem with array objects Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-10 15:56 -0700
Re: Problem with array objects Ian Collins <ian-news@hotmail.com> - 2011-05-11 12:25 +1200
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-11 10:56 +0100
Re: Problem with array objects Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-11 14:09 -0700
Re: Problem with array objects "Paul" <pchristor@yahoo.co.uk> - 2011-05-12 13:31 +0100
csiph-web