Paul said:
Consider the following:
void* p1 = (void*)new int;
void* p2 = (void*)new int[55];
p1 points to a single integer.
p2 points to an array of integers.
This is what I mean when I say 'ip' is a pointer to an array. I don't
mean it's type, I am talking about what it points to.
Oh ... uh... I'm not sure how to discuss this.
When you say "I don't mean its type" - do you mean to imply that you are now
no longer talking about/referring to the C/C++ language definition, but
something else?
Because, if we're not talking about the type of a variable, then I'm not
quite
sure what we're talking about, and I think, the language definition is
pretty clear that variables have types and those types are profoundly
meaningful
to the semantic operations of the language.
That is, I'm not sure exactly how to talk about something if we have decided
to throw out the language definition.
I suppose we can introduce new ideas, but, I think, at that point, we're not
talking about C/C++ any more, but something else.... which really isn't
on-topic.
In C++ a pointer type int* can point to:
A single integer or an array of integers.
Null.
Some random uninitialised memory.
I really don't understand that... I'm sorry, but I don't get it.
I believe a statement that conforms with the language definition might be
that a variable of type `int *' can point to an `int'. The value of
that variable
might be any valid address or NULL.
That is, the variable would be a box, and that box can contain the location
of another `int' box, or the 'special' value NULL.
I think that is a characterization that more closely follows the language
definition.
Remember what our definition of an array is. Just a sequence of
objects in memory and nothing more.
If that sequence of objects are int types then the pointer to point to
them must be of type int*.
I'm sorry, but I think I have to disagree with some of that. I do
agree on our
definition of an array.
I think I would say that a pointer to one of the elements of the array would
necessarily be an `int *'. But, in C/C++, it's very easy to construct a
pointer
to the entire array, which is quite different. For example:
int (*ap)[5];
is a pointer to an entire collection of 5 `int' elements. It does not
point to
single element, but an entire collection of 5 `int' elements. It is a
pointer-to-an-array.
The type is very meaningful, and very important, especially in the context
of operations (indexing, for example) on the array.
Also we agreed that 'arr' is a name trhat refers to this array of
objects so with:
int arr[66];
int* p = arr;
p is now a pointer that refers to the same array of integer objects
that 'arr' referred to.
We did agree that 'arr' is a name that refers to the array of objects.
However, we also agreed that in this context, the name 'arr' does not
refer to the entire set of elements.
We agreed that referencing that name in this context did not produce
the array of objects, but instead produced the address of the first element
of the array.
So - in the statement:
int *p = arr;
the 'arr' there does not refer to the entire set of elements - it refers
only
to the address of the first element. I do recall you agreeing with
that... is
that not right?
So - I believe we've been here before, and we agreed that 'p' points to
the first
element of the array. The type of 'p' is clearly an `int *' (pointer
to int), and since
it points to the first element of the array, this is all consistent.
I cannot say 'p' points to the entire array, because that is not its type.
I can say that 'p' points to the first element; I believe that's all the
C/C++
standard and common practice say in this matter.
I can also agree that the starting address of the entire array, and the
starting
address of the first element are the same, since the array is a contiguous
collection of same-typed elements.
Because of this, I might be tempted to say 'p' points to the start of
the array,
which is also true.
But 'p' is not a pointer-to-an-array, it is a pointer-to-int that
happens to be
pointing to the first element of an array of 5 `int' elements.
I believe that would be the terminology that is most accepted and most
closely
matches the language definition.
I had hoped to explain why this is important, by going on to discuss the
indexing
operator and how that relates to pointer arithmetic (which is why
getting the
type correct is critical.)
But, if we cannot agree to stick with the language definition, then it
is going
to be difficult to get a firm understanding of subsequent concepts.
Would you like to continue, using the C/C++ language definitions, or should
we agree to disagree here?
- Dave Rivers -