Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #21419

Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.

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> (permalink)
Date Sun, 27 May 2012 14:11:00 -0400
From pete <pfiland@mindspring.com>
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 <d8247fad-17c7-4105-82c1-364f00dac222@p27g2000vbl.googlegroups.com> <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

Show key headers only | View raw


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


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