P
Paul
This discussion about pointers to arrays needs to be cleared up , there are
too many people around here posting incorrect technical terms and
misrepresenting them as technically correct..
Firstly let me point out what the C++ std states about pointers, from 3.9
Basic concepts:
"3 A pointer to objects of type T is referred to as a "pointer to T.""
Its follows from that if the objects pointed to are of type float a pointer
to them is a pointer to float, float*.
So lets see what a pointer to an array of floats looks like:
float arr_flts[64]={0};
float* p = arr_flts;
p is a pointer to an array of float objects. And correctly so as defined in
the standards, this can be further proven by dereferencing p in the
following:
float var_x = p[0];
The var_x will now correctly hold the value stored in the array at index 0.
This proves that when we dereference p we access the array of floats. This
is probably obvious to most people but perhaps not to all.
So it all seems quite straight forward, and everything works as it should. A
pointer to a sequence of T's, or an array of T's is actaully of type T*. But
some people disagree.
Some people have been arguing that p is not a pointer to the array but it
is only a pointer to a single element within the array. I have described
this as nonsense because given the bunch of bananas example , you cannot
point to a banana without also pointing at the bunch.
However they insist that a pointer to an array is of the following type:
float (*pp)[64] = &arr_flts;
This does not work because if this was a pointer to an array of floats, it
would return a float when derefenced but it doesn't . This pointer-type
points to an address, it is just a fancy pointer to pointer.
So this is not a pointer to an array of floats, it is a pointer to a
pointer, for example:
cout<< *pp;
The above will output an address, which seems to prove that pp points to
another pointer.
This pointer type is a pointer to objects of array-type, that is a pointer
to arrays of arrays or a single array.
But when we say this pointer points to a single array, the term array is
used in a different context. We are now talking about an array-type, not a
sequence of float objects. The array-type object we are now talking about
is bascially another pointer-type object, its not the entity that is the
array of floats.
So is it correct to say that a char* can only point to a single char and not
to an array of chars?
No their argument is fundamentally flawed because they are misinterpreting
the context of "an array of chars" from that meaning "a sequence of char
type objects" to mean an array-type object.
too many people around here posting incorrect technical terms and
misrepresenting them as technically correct..
Firstly let me point out what the C++ std states about pointers, from 3.9
Basic concepts:
"3 A pointer to objects of type T is referred to as a "pointer to T.""
Its follows from that if the objects pointed to are of type float a pointer
to them is a pointer to float, float*.
So lets see what a pointer to an array of floats looks like:
float arr_flts[64]={0};
float* p = arr_flts;
p is a pointer to an array of float objects. And correctly so as defined in
the standards, this can be further proven by dereferencing p in the
following:
float var_x = p[0];
The var_x will now correctly hold the value stored in the array at index 0.
This proves that when we dereference p we access the array of floats. This
is probably obvious to most people but perhaps not to all.
So it all seems quite straight forward, and everything works as it should. A
pointer to a sequence of T's, or an array of T's is actaully of type T*. But
some people disagree.
Some people have been arguing that p is not a pointer to the array but it
is only a pointer to a single element within the array. I have described
this as nonsense because given the bunch of bananas example , you cannot
point to a banana without also pointing at the bunch.
However they insist that a pointer to an array is of the following type:
float (*pp)[64] = &arr_flts;
This does not work because if this was a pointer to an array of floats, it
would return a float when derefenced but it doesn't . This pointer-type
points to an address, it is just a fancy pointer to pointer.
So this is not a pointer to an array of floats, it is a pointer to a
pointer, for example:
cout<< *pp;
The above will output an address, which seems to prove that pp points to
another pointer.
This pointer type is a pointer to objects of array-type, that is a pointer
to arrays of arrays or a single array.
But when we say this pointer points to a single array, the term array is
used in a different context. We are now talking about an array-type, not a
sequence of float objects. The array-type object we are now talking about
is bascially another pointer-type object, its not the entity that is the
array of floats.
So is it correct to say that a char* can only point to a single char and not
to an array of chars?
No their argument is fundamentally flawed because they are misinterpreting
the context of "an array of chars" from that meaning "a sequence of char
type objects" to mean an array-type object.