Afficher un message
Vieux 06/02/2008, 00h24   #2
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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")
  Réponse avec citation
 
Page generated in 0,04316 seconds with 9 queries