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
}
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
}
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.
nope, C is a 0 based language
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.
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;
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.
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; };
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.