Search code examples
cdata-structures

Which number, 0 or 1, should I define the variabliable of loops for arrays from?


I'm a beginner in data structures. I learnt that SqLists are starting from 1, when the arrays inside SqLists are from 0.
The question is if I create a loop, which of the following representation is better? Will the other one be weird?

//i=0 according to arrays
for(int i=0;i<L->length;++i)
L->data[i]=givenArray[i];
//i=1 according to SqList
for(int i=1;i<=L->length;++i)
L->data[i-1]=givenArray[i-1];

Solution

  • There is nothing wrong with either ways.
    With that aside, the first way of writing is more clear and often used when writing programs (in any language).

    If you are programming alone, it doesn't really matter, just pick your style and stick to it.
    But, if you want to adhere to global programming conventions, and make your code more readable and accessible to other people, stick with the first style.