Re: Problem with array objects
"A. Bolmarcich" <aggedor@earl-grey.cloud9.net> wrote in message
news:slrnire0ks.gb4.aggedor@earl-grey.cloud9.net...
You are dereferencing an array-type , not an array.
For example:
int arr[40] ={0};
int* parr = arr;
int (*pparr)[40] = &arr;
std::cout<< "address of arr[0]:\t" << &arr[0] <<std::endl;
std::cout<< "value of *parr:\t\t" << *parr <<std::endl;
std::cout<< "value of *pparr\t\t" << *pparr <<std::endl;
Dereferencing pparr does not access the array it accesses a temporary
pointer.
The pointer pparr does not reference the array, it references an
array-type
object.
Dereferencing pparr accesses an array in the same sense that using
the name arr accesses an array.
No it doesn't
*arr =1; /*Accesses the array*/
*pparr=1; /*Cannot do this*/
Derefencing pparr gets you arr. To get what you get by
dereferencing arr, you need to dereference what you get by
dereferencing pparr. The equivalent of
*arr = 1;
is
**pparr = 1;
What happens in both cases is that the array resulting from the
expressions arr and *pparr is treated as a pointer to the first
element of the array. The assginments made to *arr and **pparr
are made to the first elements of the array.
As I said pparr needs to be dereferenced twice to access the array,
derefernecing pparr once doesn't access the array. So it points to an
array-type object , that must be convereted before the array can be
accessed.
[snip]
The (implementation defined) values returned by typeid(a1i).name()
and typeid(*pa1i).name() are the same. The output indicates that
both ai1 and *pai1 are arrays of 4 ints.
No it outputs the type, not the pointed to object.
*pa1i does not derefernece a pointer to the array, it dereferenes a
pointer
to a tempory object of array-type.
Basically its a fancy pointer to a pointer.
I never said that typeid outputs the pointed to object. The
declaration was
int a1i[4], (*pa1i)[4] = &a1i;
pa1i is a pointer to an array. *pa1i dereferences it resulting in
an array. The only objects here are an array (named a1i) and a
pointer to array (named pa1i). There is no pointer to a temporary
object here.
No there are four integer objects here, that you fail to acknowledge.
If you want to count the four subobjects of the array, keep in
mind that they are subobjects of the array rather than objects with
a lifetime independent of the array. The important point is that
there is no pointer to a temporary object here.
[snip]
The following pointer-type
int (*p)[4] =&a1i;
Does not access the array when dereferenced, it accesses an array-type
object. This object is bascially another pointer to the array.
Pointing to an array-type object, is not the same as pointing to the
array
of ints. It points to AN array , yes an array-type object( which is
basically another pointer under the hood), it doesn't point to THE
array
of
ints.
Here, the object named p is a pointer to the array named a1i.
Dereferencing it results that array. The expression
No it doesn't , dererencing this pointer results in a non modifiable
object
that cannot be used to access the array unless converted.
So it results in something that can be converted to a pointer to the
array.
The result of derefencing the pointer is the same as using the name
of the array that the pointer is pointing to. Both of the
expressions (*p) and a1i have the same result. (*p) needs to be
converted to access the array no more than a1i does.
(*p)[int_offset]
access the int_offset element of that array.
[snip]
When you dereference p1i, you create a temporary array-type object the
value
of which is another memory address. You do not get an array of 4 ints,
example:
int arr[4] = {1,2,3,4};
int (*p)[4] = &arr;
std::cout<< *p;
The type of p is an array-type, this means it points to an object of
array-type, not an array of int objects.
There is no temporary array-type object the value of which is
another memory address. There are two objects: an array named arr
and a pointer to array named p. Due to the initialization, p points
the array of int named arr.
Again you fail to acknoweldge the four integer objects.
Where is this object that you describe as "the array named arr" strored?
As I wrote above, if you want to count the four subobjects of the
array, keep in mind that they are subobjects of the array rather
than objects with a lifetime independent of the array. The
important point is that there is no temporary object here.
So where does the memory address value come from? Its not stored in the
array of integer objects.
The array named arr is stored wherever the C++ implementation decides
to store it, as long as the program behaves as specified by the C++
standard. The same is true of the pointer to array named p.
Dereferencing pparr once , yields an object which has a value that is the
address of the array. This object must exist someplace prior to it getting
converted to a pointer.
[snip]
According to the C++ standard an n-dimensional array is converted to
a pointer to an (n - 1)-dimensional array. The result of that
conversion on a 2d array is a pointer to 1d array. A pointer to a
1d array points to a 1d array, not to a 2d array. You appear to be
confusing C++ with a programming language in which arrays are
polymorphic over dimensions.
You are confusing the whole context of the statement in the standard.
It doesn't mean the array is converted to a pointer that no longer
points
to
the array.
I'm not confusing anything. I accept what the C++ standard states.
Here, it states that a conversion is done from an array to a pointer
to an element of the array. What is pointed to is an element of the
array, not the array.
You must be pointing to the array , to point to an element. Where is this
element stored if not witihin the array?
Elements of an array are stored in the region of storage created for
that array. In C++, a pointer to an array and a pointer to an
element of that array are not the same; they have different types.
The assignment
A pointer to an array of integers , is of type int*.
A pointer to an array, in your context, is a pointer to an array-type
object. Which must be dereferenced twice to access the array, the second
dereference will implicitly convert this object to an int*.
*p = 1
is allowed if p is a pointer to an element of an array of int but
not if p is a pointer to an array of int.
A pointer to an array of integers can be 2 different tyes of pointer:
a) A pointer to integers.
b) A pointer to an array-type object.
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.