Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <4FC26E34.56C@mindspring.com> (permalink) |
|---|---|
| Date | 2012-05-27 14:11 -0400 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| Newsgroups | comp.lang.c |
| Subject | Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. |
| References | <d8247fad-17c7-4105-82c1-364f00dac222@p27g2000vbl.googlegroups.com> <4657521a-93ce-465f-a76e-8aa6854558c3@w24g2000vby.googlegroups.com> |
John Reye wrote:
>
> For a concrete example, consider the following:
> Do you prefer fill_array1() or fill_array2()
>
> #define ARR_ELEMS 4
>
> struct st1
> {
> char * name;
> int i;
> int ringbuf[ARR_ELEMS];
> unsigned ringbuf_current_index;
> };
>
> #define ST1_ELEMS 10
>
> struct st1 st[ST1_ELEMS];
> unsigned statistics[ST1_ELEMS];
> /* | */
> /* common index */
>
> char *gen_name()
> {
> static char a[] = "asdf";
> return a;
> }
>
> int gen_value()
> {
> static int a = 0;
> return a++;
> }
>
> unsigned inc_ringbuf_index(unsigned i)
> {
> if (++i == ARR_ELEMS)
> return 0U;
> return i;
> }
>
> void fill_array1(unsigned i)
> {
> st[i].i = i;
> st[i].name = gen_name(i);
> st[i].ringbuf[ st[i].ringbuf_current_index ] = gen_value();
> st[i].ringbuf_current_index =
> inc_ringbuf_index( st[i].ringbuf_current_index );
> statistics[i]++;
> }
>
> void fill_array2(unsigned i)
> {
> struct st1 * const pst = &st[i]; // st + i
> unsigned idx = pst->ringbuf_current_index;
>
> pst->i = i;
> pst->name = gen_name(i);
> pst->ringbuf[ idx ] = gen_value();
> pst->ringbuf_current_index = inc_ringbuf_index( idx );
> statistics[i]++;
> }
The array addressing style that I would use
depends on the code is doing.
If (i) remains unchanged in a function,
such as you have shown in your fill_array functions,
then I might be inclined to go with fill_array2.
But, if I were cycling through the array
for (i = 0; ST1_ELEMS > i; ++i) {
puts(st[i].name);
}
then I would use the first array addressing style instead.
--
pete
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. John Reye <jononanon@googlemail.com> - 2012-05-27 10:30 -0700
Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. John Reye <jononanon@googlemail.com> - 2012-05-27 10:35 -0700
Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. pete <pfiland@mindspring.com> - 2012-05-27 14:11 -0400
Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-05-27 14:01 -0400
Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. pete <pfiland@mindspring.com> - 2012-05-27 14:20 -0400
Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. Thomas Richter <thor@math.tu-berlin.de> - 2012-05-27 22:03 +0200
Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-05-28 14:54 +0200
Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-31 13:09 -0700
csiph-web