Machine-translated by Google. Questions are authored in English.
Top

Discussion

The first index number in an array starts with................and the index number of an array of size n will be :

  • A.0, n - 1
  • B.1, n - 1
  • C.0, n
  • D.1, n

Answer: A

The first index is 0 and the last is n - 1, so the answer is 0, n - 1.

In C and C++, an array index is an offset from the start of the array. The first element is at offset 0, so it is a[0]. An array of size n therefore has elements a[0] to a[n-1], which is n elements in all.

Starting at 1 is the convention of languages such as Fortran, not C. Using n as the last index reads one element past the end of the array, a classic off-by-one error that causes undefined behaviour.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.