Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. What to call array indexing starting at 0 vs. 1 ?

What to call array indexing starting at 0 vs. 1 ?

Scheduled Pinned Locked Moved The Lounge
c++delphidatabasevisual-studiodata-structures
70 Posts 36 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G glennPattonWork3

    Standard & Mutant ? why was Pascal 1 based? anyone know if Delphi is has one based arrays as it might explain some error generated by a test application I have to use?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #26

    glennPattonWork wrote:

    why was Pascal 1 based?

    It's not, neither is Object-Pascal (Delphi). The type-definition of an array would contain the start-index and the end-index. One would use 0, obviously. Dynamic arrays start at 0. Even BASIC[^] starts at 0.

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

    1 Reply Last reply
    0
    • N Nagy Vilmos

      Meh! Real men code with edlin.

      speramus in juniperus

      G Offline
      G Offline
      Gary Wheeler
      wrote on last edited by
      #27

      Real men code by staring intently at their powered-off, disconnected hard drive and visualize the magnetic bits into their proper alignment.

      Software Zen: delete this;

      1 Reply Last reply
      0
      • N Nagy Vilmos

        Meh! Real men code with edlin.

        speramus in juniperus

        J Offline
        J Offline
        jeron1
        wrote on last edited by
        #28

        Nagy Vilmos wrote:

        Real men code with edlin.

        I said something like this to a younger colleague a few weeks ago, I got a 'deer in headlight' look as a response. God I'm old :(( .

        1 Reply Last reply
        0
        • N Nagy Vilmos

          Meh! Real men code with edlin.

          speramus in juniperus

          F Offline
          F Offline
          Forogar
          wrote on last edited by
          #29

          Quote:

          Real men code with edlin.

          oh yeah!

          - I would love to change the world, but they won’t give me the source code.

          1 Reply Last reply
          0
          • N Nagy Vilmos

            I believe the correct terms are right and wrong.

            speramus in juniperus

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #30

            That was true before Lua appeared.

            Veni, vidi, vici.

            1 Reply Last reply
            0
            • M Maximilien

              Is there a technical term to say if an array starts at index 0 (e.g. C/C++) or 1 (e.g. pascal) ? Is it simply "0-based array" and "1 based array" ? Thanks.

              I'd rather be phishing!

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #31

              Maximilien wrote:

              Is it simply "0-based array" and "1-based array" ?

              Yes.

              Veni, vidi, vici.

              1 Reply Last reply
              0
              • N Nicholas Marty

                I only have seen that in the Intellisense popup of some .NET function. Intellisense is all the documentation that I need. :laugh:

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #32

                Nicholas Marty wrote:

                Intellisense is all the documentation that I need.

                :~

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                1 Reply Last reply
                0
                • pkfoxP pkfox

                  Real men code with Emacs and vi.

                  We can’t stop here, this is bat country - Hunter S Thompson RIP

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #33

                  EDT

                  C R R 3 Replies Last reply
                  0
                  • M Maximilien

                    Is there a technical term to say if an array starts at index 0 (e.g. C/C++) or 1 (e.g. pascal) ? Is it simply "0-based array" and "1 based array" ? Thanks.

                    I'd rather be phishing!

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #34

                    (0) The one true path to direct epiphanic experience of ineffable ecstasy in emptiness. (1) The Mother of All Lies, the Deceiver, the Abomination which is the Canker that rots the Soul from within. bill

                    Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                    pkfoxP 1 Reply Last reply
                    0
                    • B BillWoodruff

                      (0) The one true path to direct epiphanic experience of ineffable ecstasy in emptiness. (1) The Mother of All Lies, the Deceiver, the Abomination which is the Canker that rots the Soul from within. bill

                      Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

                      pkfoxP Offline
                      pkfoxP Offline
                      pkfox
                      wrote on last edited by
                      #35

                      Scary stuff Bill :thumbsup:

                      We can’t stop here, this is bat country - Hunter S Thompson RIP

                      1 Reply Last reply
                      0
                      • pkfoxP pkfox

                        Real men code with Emacs and vi.

                        We can’t stop here, this is bat country - Hunter S Thompson RIP

                        P Offline
                        P Offline
                        parths
                        wrote on last edited by
                        #36

                        I'm surprised no put thought of putting the obligatory XKCD[^] yet

                        "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman "Philosophy is a study that lets us be unhappy more intelligently." -Anon.

                        1 Reply Last reply
                        0
                        • M Maximilien

                          Is there a technical term to say if an array starts at index 0 (e.g. C/C++) or 1 (e.g. pascal) ? Is it simply "0-based array" and "1 based array" ? Thanks.

                          I'd rather be phishing!

                          E Offline
                          E Offline
                          ExcellentOrg
                          wrote on last edited by
                          #37

                          Looks like you are on a journey of self discovery, lad!!!. Keep going and you will discover the mysterious world of Big Endian and Little Endian. But Beware, Do not rest until you cross the Byte Boundary, Word Boundary and Double Word boundary. Beyond that you will start to see some faint light from the tower 64. ------------------ - Wizard of O1

                          1 Reply Last reply
                          0
                          • N Nicholas Marty

                            I only have seen that in the Intellisense popup of some .NET function. Intellisense is all the documentation that I need. :laugh:

                            S Offline
                            S Offline
                            SortaCore
                            wrote on last edited by
                            #38

                            Real men code with smoke signals and dances to the beer gods! EDIT: Just found http://gagabla.com/files/images/44_522b647487cdb.jpg[^]

                            1 Reply Last reply
                            0
                            • N Nagy Vilmos

                              Meh! Real men code with edlin.

                              speramus in juniperus

                              B Offline
                              B Offline
                              BugMakerPhil
                              wrote on last edited by
                              #39

                              sure ! I've use edlin on MS-DOS, e on OS/2, etc... but in the 80' :laugh: now, Notepad++ fits to my needs

                              If there is no solution, there is no problem !

                              1 Reply Last reply
                              0
                              • M Maximilien

                                Is there a technical term to say if an array starts at index 0 (e.g. C/C++) or 1 (e.g. pascal) ? Is it simply "0-based array" and "1 based array" ? Thanks.

                                I'd rather be phishing!

                                D Offline
                                D Offline
                                daniel_bingamon
                                wrote on last edited by
                                #40

                                "0-based array" is called an "offset". "1-based array" could be called "displayable index"

                                1 Reply Last reply
                                0
                                • M Maximilien

                                  Is there a technical term to say if an array starts at index 0 (e.g. C/C++) or 1 (e.g. pascal) ? Is it simply "0-based array" and "1 based array" ? Thanks.

                                  I'd rather be phishing!

                                  K Offline
                                  K Offline
                                  kalberts
                                  wrote on last edited by
                                  #41

                                  So you have never been programming Pascal. As others have pointed out, in Pascal you set both the lower and upper index limits. (Furthermore, indexes may be any scalar type, such as an enumeration value. Enums are NOT names of integers in Pascal, but distinct value domains.) You probably confused Pascal with Fortran, where array indexes start at 1. I never understood this fascination for low-level programming in high-level languages! Say, if my table contains data for the years 1960 to 2020, why shouldn't I address (index) the elements with values from 1960 to 2020? Why would I want to define an index base as a separate constant and for every indexing operation subtract this base from the "real" selector value to get to the right index? Calculating addresses is the job of the compiler (and linker), isn't it? That's why we use a high level language compiler! Having to subtract some base index value is like abandoning struct (/record) mechanisms and decalre the variable as a byte array, field names being offsets into the array. That gives you full control over packing and that kind of things, doesn't it? Great! Well, we do that in assembler, and it works. We can even do it in machine independent assembler, aka "C". That's what we called C when it appeared, "machine independent assembler". I had my first university level programming education in Pascal, and we saw C as a great step backwards for modelling problem domain concepts. I still do. Some languages, such as C#, allows you to define the semantics of the [] acessor for your own data types, so that the user of your class may index by values from 1960 to 2020. But you have to do a lot of programming to achieve this, and although I haven't checked, I am quite sure that the compiler isn't smart enough to reduce your accessor function to a simple subtraction of a base index value, the way the Pascal compiler could (not requiring any sort of programming from you). Technology isn't always moving in the forwards direction!

                                  S E M 3 Replies Last reply
                                  0
                                  • K kalberts

                                    So you have never been programming Pascal. As others have pointed out, in Pascal you set both the lower and upper index limits. (Furthermore, indexes may be any scalar type, such as an enumeration value. Enums are NOT names of integers in Pascal, but distinct value domains.) You probably confused Pascal with Fortran, where array indexes start at 1. I never understood this fascination for low-level programming in high-level languages! Say, if my table contains data for the years 1960 to 2020, why shouldn't I address (index) the elements with values from 1960 to 2020? Why would I want to define an index base as a separate constant and for every indexing operation subtract this base from the "real" selector value to get to the right index? Calculating addresses is the job of the compiler (and linker), isn't it? That's why we use a high level language compiler! Having to subtract some base index value is like abandoning struct (/record) mechanisms and decalre the variable as a byte array, field names being offsets into the array. That gives you full control over packing and that kind of things, doesn't it? Great! Well, we do that in assembler, and it works. We can even do it in machine independent assembler, aka "C". That's what we called C when it appeared, "machine independent assembler". I had my first university level programming education in Pascal, and we saw C as a great step backwards for modelling problem domain concepts. I still do. Some languages, such as C#, allows you to define the semantics of the [] acessor for your own data types, so that the user of your class may index by values from 1960 to 2020. But you have to do a lot of programming to achieve this, and although I haven't checked, I am quite sure that the compiler isn't smart enough to reduce your accessor function to a simple subtraction of a base index value, the way the Pascal compiler could (not requiring any sort of programming from you). Technology isn't always moving in the forwards direction!

                                    S Offline
                                    S Offline
                                    Sentenryu
                                    wrote on last edited by
                                    #42

                                    Member 7989122 wrote:

                                    if my table contains data for the years 1960 to 2020,

                                    If you hardcode that, you're doing it wrong. That case is what hashtables are for. Also, in C# you can use Array.CreateInstance to make arrays with arbitrary bounds and dimensions. You might want to start programming in COBOL.

                                    I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                                    K 1 Reply Last reply
                                    0
                                    • K kalberts

                                      So you have never been programming Pascal. As others have pointed out, in Pascal you set both the lower and upper index limits. (Furthermore, indexes may be any scalar type, such as an enumeration value. Enums are NOT names of integers in Pascal, but distinct value domains.) You probably confused Pascal with Fortran, where array indexes start at 1. I never understood this fascination for low-level programming in high-level languages! Say, if my table contains data for the years 1960 to 2020, why shouldn't I address (index) the elements with values from 1960 to 2020? Why would I want to define an index base as a separate constant and for every indexing operation subtract this base from the "real" selector value to get to the right index? Calculating addresses is the job of the compiler (and linker), isn't it? That's why we use a high level language compiler! Having to subtract some base index value is like abandoning struct (/record) mechanisms and decalre the variable as a byte array, field names being offsets into the array. That gives you full control over packing and that kind of things, doesn't it? Great! Well, we do that in assembler, and it works. We can even do it in machine independent assembler, aka "C". That's what we called C when it appeared, "machine independent assembler". I had my first university level programming education in Pascal, and we saw C as a great step backwards for modelling problem domain concepts. I still do. Some languages, such as C#, allows you to define the semantics of the [] acessor for your own data types, so that the user of your class may index by values from 1960 to 2020. But you have to do a lot of programming to achieve this, and although I haven't checked, I am quite sure that the compiler isn't smart enough to reduce your accessor function to a simple subtraction of a base index value, the way the Pascal compiler could (not requiring any sort of programming from you). Technology isn't always moving in the forwards direction!

                                      E Offline
                                      E Offline
                                      englebart
                                      wrote on last edited by
                                      #43

                                      In C you could do this:

                                      int arrayMemory[2020-1960+1];
                                      int *arrayAccess = arrayMemory - 1960; // int* arithmetic

                                      int i;
                                      for (i=1960; i<=2020; ++i) {
                                      arrayAccess[i] = ...;
                                      }

                                      C is high level assembler. If indexing something by 1 makes sense for a piece of code you are writing, then just allocate one extra slot of memory and ignore index 0. I have done that with date based logic before. P.S. The fact that January is considered month 0 in Java still irks me. Whatever happened to encapsulation?

                                      K 1 Reply Last reply
                                      0
                                      • S Sentenryu

                                        Member 7989122 wrote:

                                        if my table contains data for the years 1960 to 2020,

                                        If you hardcode that, you're doing it wrong. That case is what hashtables are for. Also, in C# you can use Array.CreateInstance to make arrays with arbitrary bounds and dimensions. You might want to start programming in COBOL.

                                        I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                                        K Offline
                                        K Offline
                                        kalberts
                                        wrote on last edited by
                                        #44

                                        Sentenryu wrote:

                                        If you hardcode that, you're doing it wrong.

                                        We all (I assume) have laughed at that old Xerox Fortran manual (yes, Xerox did make computers, long time ago), describing how the PARAMETER statement (roughly equivalent to a C #define) could be used to give a symbolic name to a numeric constant, so that you can write PI instead of 3.14159.... This will reduce typing, improve readability, and ease program maintenance, according to the manual, in case the value of PI changes. Making software general is a good principle, but it can go too far. Say, if you create a highly specialized program for processing some data related to WW2, and your array of events is indexed by years 1939 to 1945, you could ride your principles so that this program can be adapted to changing conditions, such as starting in 1952 and lasting until 1966. Sure, from an academic point of view, you gain a lot of flexibility; you then have a program system that can handle a lot of different WW2s, not only the real one, but any number of counterfactual ones. Which is valuable from an (information technology) academic point of view. I guess the historican who uses this program to manage information from the factual WW2 could care less about the counterfactual ones. He would rather relate to "1939" than to "FirstYearOfWar" - is it the first year the US was involved? Or the first year of buildup of the military forces? Or the first full year of war, not counting years which were peaceful for the first months? Super-generality may be destructive both to readability and understanding. And it might complicate code, both for the programmer and compiler, as the generality opens for a large number of exceptional (and non-exceptional) cases possible in the generalized model, but which will never occur in the factual domain that you are modelling.

                                        S 1 Reply Last reply
                                        0
                                        • P PIEBALDconsult

                                          EDT

                                          C Offline
                                          C Offline
                                          cabowaboaddict
                                          wrote on last edited by
                                          #45

                                          PIEBALDconsult wrote:

                                          EDT

                                          :thumbsup::thumbsup::cool:

                                          B 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups