Are there any problems starting the array index at 1?

My teacher usually starts indexing arrays from one. So basically, when we need an array of 100 elements, it uses

int a[101] 

instead

int a[100]

and, for example, it fills it as follows:

for (int i = 1; i <= 100; i++)
    cin >> a[i];

Are there any problems using this method or should I avoid it? (I have no problem working with indexes starting at 0)

+4
source share
3 answers

Should I use this regularly, or should I avoid it?

. , 99,9% ++ , , . . , , , container.begin() container.end(), std::begin() std::end() C ++, 0.

. , begin()/end(), . , , .

+16

++ C , 0, N-1 []. - , a[0], , a[1], .

+1

?

int . , 16 , int 2 . 4 32-, 64- .

int

cout << sizeof(int);


: sizeof (int) = 4 .

?


int array[10];
40 BYTES ( ).

?

9 (1-9), 36 40 4 . . , 3- .
:

  int array[100][100][100]

As a result, you will lose a lot of memory. I mean a lot of memory, which another process requires, but cannot use only because you intentionally reserved it and did not use it.

The problem with the lines.

If you work with arrays of characters, you can land something in a disaster.
Example:

    char str[8];
    str[1] = 'H';
    str[2] = 'e';
    str[3] = 'l';
    str[4] = 'l';
    str[5] = 'o';
    str[6] = '\0';
    cout<<str;

There may be a situation where the output will not be displayed, because it str[0]may contain a character NULL.

-1
source

Source: https://habr.com/ru/post/1690806/


All Articles