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

"RaZiel" <(E-Mail Removed)> wrote in message
news:im2ctv$e7$(E-Mail Removed)...
> 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.
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?


 
Reply With Quote
 
 
 
 
RaZiel
Guest
Posts: n/a
 
      03-19-2011
On 19.03.2011 15:39, Paul wrote:
>
> "RaZiel" <(E-Mail Removed)> wrote in message
> news:im2ctv$e7$(E-Mail Removed)...
>> 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?
> 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
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      03-19-2011

"RaZiel" <(E-Mail Removed)> wrote in message
news:im2hrr$ce5$(E-Mail Removed)...
> On 19.03.2011 15:39, Paul wrote:
>>
>> "RaZiel" <(E-Mail Removed)> wrote in message
>> news:im2ctv$e7$(E-Mail Removed)...
>>> 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.

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


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.

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

TY , you have a nice day too whatever you chose to do .

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

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

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

> 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[i], 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.


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

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:im2qgh$vgc$02$(E-Mail Removed)-online.com...
> Paul wrote:
>
>>> 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;


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

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 Bublic A{~B(){std::cout<<"B destructor\n";}};
class Cublic 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.




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

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[i], 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.



 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      03-21-2011

"Paul" <(E-Mail Removed)> wrote in message
news:ff8hp.196005$(E-Mail Removed)2...
>
> "Rolf Magnus" <(E-Mail Removed)> wrote in message
> news:im2qgh$vgc$02$(E-Mail Removed)-online.com...
>> Paul wrote:
>>>> 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.

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.

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

>"Fred Zwarts" <(E-Mail Removed)> wrote in message
>news:im72jf$4l3$(E-Mail Removed)...


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








 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      03-21-2011

"Paul" <(E-Mail Removed)> wrote in message
news:hTGhp.72868$%(E-Mail Removed)2...
>
>>"Fred Zwarts" <(E-Mail Removed)> wrote in message
>>news:im72jf$4l3$(E-Mail Removed)...

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

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

"Fred Zwarts" <(E-Mail Removed)> wrote in message
news:im7ee1$1il$(E-Mail Removed)...

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


 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      03-21-2011

"Paul" <(E-Mail Removed)> wrote in message
news:BgHhp.188127$(E-Mail Removed)2...
>
> "Fred Zwarts" <(E-Mail Removed)> wrote in message
> news:im7ee1$1il$(E-Mail Removed)...
>
> <snip>
>>>>>>> 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?

 
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