Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 27 May 2012 13:11:02 -0500 Message-ID: <4FC26E34.56C@mindspring.com> Date: Sun, 27 May 2012 14:11:00 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc. References: <4657521a-93ce-465f-a76e-8aa6854558c3@w24g2000vby.googlegroups.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 79 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.221.107 X-Trace: sv3-Lhv/+QxEapmQSFVwCFq+sBB2H5d6VL4X4ttkEO/sZm9TU7pFqUZyveyZl2SG+yYsol/IhIEXe1XDWOs!piNDNF1FaI5YLeM/rIDVI3PO9B/cc0cjxN85f2Ow9vARtPWyqOCbLPo8nWIaRrSZoU8oKajMhczP!4iZHxstz0Q== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2880 X-Received-Bytes: 3021 Xref: csiph.com comp.lang.c:21419 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