[EMAIL PROTECTED] wrote:
>
> You are certainly correct in pointing out that pointers and indexes can be
> converted into each other. But you do have to be careful:
>
> I am sure you agree with me that if i is an index into a buffer,
>
> int i; char buf[10], *p;
>
> then trying to access the element at buf[i] is quite something different
> from using the index as a pointer, *i. That would be a huge bug.
>
I completely agree. I see this as another case of balance, where one
can go wrong two ways:
* by thinking that indexes and pointers are totally unrelated
(thereby missing opportunities to understand the language better)
* by having an over-simplified view that wallpapers over important
but subtle distinctions (thereby committing a variety of bugs,
including some you mentioned)
>
> And both Kernighan AND Ritchie would be surprised if given
>
> char buf [10], *p;
>
> I were to say:
>
> for (buf = p; *buf; buf++) { printf ( "5c", *buf ) }
>
> in place of the 'for expression I used above.
>
In the next few sentences they explain why that is illegal, stating
that an array name is to be regarded as a constant, and therefore
cannot be modified.
(To me, that is a virtue of a good model; there are a minimum of
exceptions, and the ones that do exist can be handled succinctly:
"An array name is equivalent to a pointer to the first element,
except that it is a constant.")
As for the rest of your post, I believe it well illustrates some
"important but subtle distinctions" in the subject of references.
I'd like to think about it a little more, and then offer some
feedback.
-jn-