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


Groups > comp.lang.c > #77041

Re: just a curiosity about starting array indexing at 0 as opposed to 1

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.lang.c
Subject Re: just a curiosity about starting array indexing at 0 as opposed to 1
Date 2015-11-23 23:08 +0000
Organization Aioe.org NNTP Server
Message-ID <n306a1$cgi$1@speranza.aioe.org> (permalink)
References (1 earlier) <v9d4y.153669$fC2.133336@fx03.iad> <n2slak$45o$1@speranza.aioe.org> <9_k4y.237864$af1.94488@fx04.iad> <n2v5cv$shg$1@speranza.aioe.org> <n2v8v8$ghd$1@dont-email.me>

Show all headers | View raw


BartC <bc@freeuk.com> wrote:

(snip, I wrote)
>> One is that 0 origin is more obvious to assembly programmers
>> on most systems. That might be enough.
 
> I wonder what the language would have looked like if we'd catered for 
> all the other supposed preferences of assembly programmers?
 
> And in assembly of course, you can also choose any array start index you 
> like. But it means having to be aware of it every time you access an 
> array element and modifying the instruction as needed.

One case I know is translation tables for S/360 (and successor)
TR (translate) instruction. It is not unusual to want to translate
only the digits (X'F0' to X'F9') and not need the 240 entries 
before them. You can put -240 (or -C'0') in every TR instruction,
but you can also EQU a new symbol.

TABLE EQU *-240
      DC   C'0123456789ABCDEF'

that works as long as *-240 is addressable, which usually means not
before the beginning of the CSECT.

> Wouldn't it good if we could invent a 'higher level' language, where we 
> state the array lower bound in just one place, then we don't need to 
> worry about it every time we write a[i]?
 
(snip on other things related to non-0 origin in C)

(then I wrote)
>> Assumed shape, pass by descriptor, arrays in Fortran pretty much
>> work that way. (The origin is 1 in the called routine, unless
>> a different origin is specified.)
 
> I think Fortran didn't always have sight of the subroutine being called. 
> So neither caller nor callee would know what lower bound was used by the 
> other. I guess that's fixed now.

Assumed shape is new in Fortran 90. It passes arrays by descriptor,
such that the bounds are also passed. But as seen in the called
routine, the bounds are from 1, or an origin specified in the
called routine, not the original origin.

The only array passing method available (without cheating) in PL/I
passes by descriptor, using both lower and upper bound.
(It is a favorite trick to call Fortran, and maybe C, passing the
first element of an array by reference.)
 
>> Seems to me, then, that it isn't so hard to implement non 0 origin,
>> though it does complicate things.
 
> In C, or in general? Because I believe some languages have managed it!

Yes, I meand in C. 
 
>> An important part of early C was its simplicity. This does complicate
>> things more than necessary, especially for problems otherwise written
>> in assembly.
 
> I've been used N-based arrays in language as simple as C for many years, 
> and it has never been an issue.
 
> But I think C is stuck with 0-based simply because most people are 
> against it.

I have converted some 1 origin based programs to C, and it takes much
longer than might be expected, to get the arrays right.

-- glen

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

just a curiosity about starting array indexing at 0 as opposed to 1 G G <gdotone@gmail.com> - 2015-11-21 18:50 -0800
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 supercat@casperkitty.com - 2015-11-21 19:31 -0800
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-22 00:15 -0800
      Re: just a curiosity about starting array indexing at 0 as opposed to 1 supercat@casperkitty.com - 2015-11-22 07:20 -0800
        Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-22 16:22 +0000
          Re: just a curiosity about starting array indexing at 0 as opposed to 1 supercat@casperkitty.com - 2015-11-22 08:44 -0800
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 10:37 -0500
      Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-22 15:50 +0000
        Re: just a curiosity about starting array indexing at 0 as opposed to 1 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 11:02 -0500
          Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-22 16:15 +0000
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 Ian Collins <ian-news@hotmail.com> - 2015-11-22 16:42 +1300
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 G G <gdotone@gmail.com> - 2015-11-21 19:55 -0800
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 Keith Thompson <kst-u@mib.org> - 2015-11-23 11:01 -0800
      Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-23 19:18 +0000
        Re: just a curiosity about starting array indexing at 0 as opposed to 1 Keith Thompson <kst-u@mib.org> - 2015-11-23 11:26 -0800
          Re: just a curiosity about starting array indexing at 0 as opposed to 1 glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-23 22:54 +0000
            Re: just a curiosity about starting array indexing at 0 as opposed to 1 Robert Wessel <robertwessel2@yahoo.com> - 2015-11-23 18:37 -0600
              Re: just a curiosity about starting array indexing at 0 as opposed to 1 raltbos@xs4all.nl (Richard Bos) - 2015-11-24 15:22 +0000
      Re: just a curiosity about starting array indexing at 0 as opposed to 1 raltbos@xs4all.nl (Richard Bos) - 2015-11-24 15:13 +0000
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 01:21 -0500
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-22 15:00 +0000
      Re: just a curiosity about starting array indexing at 0 as opposed to 1 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-22 10:15 -0500
        Re: just a curiosity about starting array indexing at 0 as opposed to 1 glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-23 13:47 +0000
          Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-23 14:50 +0000
            Re: just a curiosity about starting array indexing at 0 as opposed to 1 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-23 12:01 -0500
              Re: just a curiosity about starting array indexing at 0 as opposed to 1 James Kuyper <jameskuyper@verizon.net> - 2015-11-23 12:12 -0500
                Re: just a curiosity about starting array indexing at 0 as opposed to 1 Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-23 12:57 -0500
            Re: just a curiosity about starting array indexing at 0 as opposed to 1 glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-23 23:08 +0000
          Re: just a curiosity about starting array indexing at 0 as opposed to 1 bert <bert.hutchings@btinternet.com> - 2015-11-23 08:52 -0800
      Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-22 15:42 +0000
        Re: just a curiosity about starting array indexing at 0 as opposed to 1 Keith Thompson <kst-u@mib.org> - 2015-11-22 12:36 -0800
        Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-22 21:06 +0000
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-22 10:28 +0000
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 gazelle@shell.xmission.com (Kenny McCormack) - 2015-11-22 12:04 +0000
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-22 11:24 +0000
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 G G <gdotone@gmail.com> - 2015-11-22 04:29 -0800
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 see.my.homepage@gmail.com - 2015-11-22 04:54 -0800
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 BartC <bc@freeuk.com> - 2015-11-22 13:43 +0000
    Re: just a curiosity about starting array indexing at 0 as opposed to 1 James Kuyper <jameskuyper@verizon.net> - 2015-11-22 11:04 -0500
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 Jens Stuckelberger <Jens_Stuckelberger@nowhere.net> - 2015-11-22 15:29 +0000
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-22 14:06 -0800
  Re: just a curiosity about starting array indexing at 0 as opposed to 1 John Bode <jfbode1029@gmail.com> - 2015-11-23 07:49 -0800

csiph-web