Re: Neatest way to get the end pointer?
On Feb 6, 4:01 am, Peter Nilsson <ai...@acay.com.au> wrote:
> vipps...@gmail.com wrote:
> > Peter Nilsson <ai...@acay.com.au> wrote:
> > > ... one byte past the end of an array _is_ a valid
> > > pointer.
>
> > Hehe, sorry mr Keith for doubting :-P.
>
> > However, as you said mr Nilsson, he (the OP) is then
> > dereferencing it, and invoking undefined behavior.
>
> I never said the OP dereferenced it. The actual code had
> been snipped by that point. Here it is...
>
> int *p = my_array;
> int const *const pend = my_array + sizeof my_array/
> sizeof*my_array;
> do *p++ = 42;
> while (pend != p);
>
> At no stage is pend dereferenced; and the loop exits on
> p == pend, so the address is not dereferenced by p
> either.
Ah, I was a bit confused I guess.
However, OP _does_ dereference it. Not in that snipped, but in 4)
> 4) *(&my_array+ 1) decays to the address of the first element in the
> non-existant array after the current one, which is also the "pend"
> address for the array that actually exists.
|