Re: Neatest way to get the end pointer?
Tomás Ó hÉilidhe wrote:
> I commonly use pointers to iterate thru an array. For example:
>
> int my_array[X];
>
> int *p = my_array;
> int const *const pend = my_array + sizeof my_array/sizeof*my_array;
>
> do *p++ = 42;
> while (pend != p);
Use a understandable macro for the array size, then you can have pend =
my_array + X.
(Also, I'd write it as `for (p = my_array; p < pend; p++) *p = 42;`. Or
even directly p < my_array + X, since a decent compiler should optimize
that, shouldn't it?)
--
Army1987 (Replace "NOSPAM" with "email")
|