On 05/17/2013 07:01 PM, Malcolm McLean wrote:
> On Friday, May 17, 2013 5:17:16 PM UTC+1, James Kuyper wrote:
...
>> How do you access any element of an array other than the first without
>> pointer arithmetic? array[i] is defined to be behave as *(array+i), and
>> therefore does involve pointer arithmetic.
>>
> Counting on isn't usually considered to be arithmetic, though it's
> a semantic argument. Someone who knows the names, order and value
> of the numbers in his native language is not considered to be
> numerate.
I'm unfamiliar with the use of the phrase "count on" with any meaning
other than "rely upon". So are the dictionaries I've consulted. What do
you mean by that? I can't imagine a reasonable definition of "pointer
arithmetic" that excludes pointer+integer, which is, in my opinion, the
most prototypical example of pointer arithmetic.
> If you take the value of the pointer expression and store it,
> then I'd define that as pointer arithmetic.
I consider "pointer arithmetic" to refer exclusively to the use of
arithmetic operators when one of the operands is a pointer value. In
other words, pointer+integer, integer+pointer, pointer-integer,
pointer-pointer, pointer+=integer, pointer-=integer, pointer++,
++pointer, pointer--, or --pointer. Technically, that definition also
covers +pointer, but I don't want to worry about that edge case. I
consider subscripting to be an example of hidden pointer arithmetic,
since pointer[integer] is defined as being equivalent to *(pointer+integer).
Arguably, the member selection operator is another example of hidden
pointer arithmetic, because pointer->member is normally implemented in a
manner equivalent to
*(member_type*)((char*)pointer + offsetof(struct_type, member))
However, since the standard does not actually define the member
selection operator in those terms, I only mark that as "arguable".
Your definition does not require that the pointer expression involve
arithmetic operators, which I find odd, and requires that the result be
stored, which I find even odder.
The standard never defines the meaning of the phrase "pointer
arithmetic", and uses it in only one place, 6.5.6p10:
> EXAMPLE Pointer arithmetic is well defined with pointers to variable length array types.
> {
> int n = 4, m = 3;
> int a[n][m];
> int (*p)[m] = a; // p == &a[0]
> p += 1; // p == &a[1]
> (*p)[2] = 99; // a[1][2] == 99
> n = p - a; // n == 1
> }
The wording implies that the example contains pointer arithmetic on a
pointer to a VLA type. Unfortunately, both of our definitions are
consistent with that implication:
According to your definition, as I understand it, the following
expressions from the example involve pointer arithmetic on pointers to a
VLA type. I would disagree with the first one, because it doesn't
involve any arithmetic operators:
int (*p)[m] = a;
p += 1
According to my definition, the following expressions from the example
involve pointer arithmetic on pointers to a VLA type. Am I correct in
concluding that you would disagree with the second expression, because
it does not involve storage of the value of an expression with pointer type?
p += 1
p - a
I also consider (*p)[2] to be an example of hidden pointer arithmetic,
which does not meet your definition, since it doesn't involve storage of
the value of an expression of pointer type. However, since *p is not a
pointer to a VLA type, (*p)[2] couldn't be the example pointer
arithmetic whose existence is implied by the sentence at the start of
6.5.6p10.
My definition seems consistent with the uses of the phrase "pointer
arithmetic" within the wikipedia article on pointers; insofar as there's
a difference between our definitions, yours does not.
--
James Kuyper