Thread: Arrays Indexing Intialization

  1. #1
    jshamilton73
    Guest

    Arrays Indexing Intialization

    It is possible to start a C++ array at some index other than zero??
    Such as one, similar to the option in Fortran.

    e.x.

    int i, index= 1;
    double array[9];

    for (i = 1; i <=10; i++)
    {
    array[i] = index*20;
    // array doesn't start at zero

    }

  2. #2
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Not that I know of -- I'm pretty sure that zero-based arrays are just part of the language.
    My programs don't have bugs, they just develop random features.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    nope, C is a 0 based language

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    a[b] is really just shorthand for *(a+b) Thus the first elemet is always at offset +0 . Realistically there is one type of person in the world, those who start counting from zero and those who don't.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    148
    1. Just declare your arrays one element larger than needed so that the index ranges from 0 to N,and only use elements 1 to N.

    2.(ugly,unportable)
    Code:
    double array[9] = {1,2,3,4,5,6,7,8,9};
    double *pA = &(-1)[array];
    for(int i = 1;i <= 9;i++)
        cout << pA[i] << endl;

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can write a small array class that'll let you specify the starting index. It shouldn't be too hard.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    That's (part of) what classes are for. Write a [template?] class to do exactly that. You'll probably want to overload at least the operator[], but it should be easy. Sample (untested):
    Code:
    template<class T>
    class Array {
    public:
       // you can figure out constructors, and stuff
       T& operator[]( int i ) {
          return theRealArray[ i - 1 ];
       }
    private:
       T* theRealArray;
    };

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    These workarounds work, but you really should consider just using C-style array notation, instead of trying to MacGuyver a nonstandard notation.

    Once you start messing with the indexing of arrays, your code will become vastly less readable to anyone else. And you'll develop bad habits regarding indexing that will haunt you throughout your C++ career -- learning a good habit now is easier than unlearning a bad habit later.

    So the answer to your question, "CAN you do this?" Of course; C++ allows you to roll your own array-like classes. The answer to the better question to ask, "SHOULD you do this?", is a resounding NO.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  4. Indexing arrays
    By Dutch in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2002, 02:19 PM
  5. [] intialization of arrays
    By Saravanan in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 02:20 PM