answersLogoWhite

0


Best Answer

In order to provide modulo (%) facilities as far as mathematics and logic is concern.

for instance , if we have no of employees in our list say 1545 and if we want to accommodate it within small and effective range then by using % we can do it easily....

we can arrange it in the range within Array with maximum index 14 (0,1,2,.....14).

And 0,15,30,45......1545 will belongs to same category say index no 0.

Think if 0 is not included into index then where we would store 0,15,30,45....1530,1545 .

From a technical standpoint, you need to understand that an array is basically just the starting address of a chunk of memory. The "index" of the array is actually an address offset. So the value at the 0th position would be stored at the address of the array plus zero. The value at the 1st position would be stored at the address plus one, and so on.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

because they use pointer arithmetic.

The name of the array refers to the starting memory position of the array

so the first element of the array is at offset 0

int array[5];

the first element of array is at (array +0) and has the value *(array+0)

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The first element is offset 0 * size_of_element bytes from the start of the array, and is therefore index 0. The next element is offset 1 * size_of_element bytes from the start of the array and is therefore index 1. And so on.

The array name is merely an alias for the start of the array and the compiler knows the length of each element so it can easily work out the offset of every element by its index alone -- which must always be zero-based.

Some languages do permit a user-defined base, but in reality they are zero-based. The compiler or interpreter simply adjusts the index to a zero-based index by subtracting the user-defined base from all the indices. C/C++ doesn't use this feature because it is not only unnecessary, it actually makes code harder to read and maintain due to inconsistent index bases. In C/C++, all indices are zero-based. Always.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

It is more convenient. An arrays is actually a pointer and foo[1] would be the second element in the array. Another way of writing foo[1] is *(foo + 1) so *(foo + 0) would naturally be the first element and *(foo + 0) is the equivalent of *foo.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It really depends on the programming language; in some languages, the index starts from 0, in others from 1. Any starting point might be used, but it makes sense to use either 0 or 1; which one is used is a more or less arbitrary decision. Some languages even let you define an arbitrary range such as [5 ... 10], for example, when defining the array.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Array subscripts always start from zero because the first element is always at offset address 0 from the start of the array. We calculate the address of each element by multiplying its subscript (or index) by the size of the array type. Thus, for an array of 4-byte integers, the first element will be found 0*4 bytes from the start of the array, the next will be 1*4 bytes, the next will be 2*4 bytes, and so on. Since the size of the array type can be determined at compile time, the compiler is able to calculate the offsets from the index alone and is thus able to provide subscript notation to the programmer. This is merely sugar-coating; Behind the Scenes, the compiler generates the appropriate pointer arithmetic for us.

Not all languages use zero-based arrays. Some use a one-based array by default and/or allow the programmer to choose the base index. Again, this is simply sugar-coating. Behind the scenes, the compiler will convert the index to a zero-based index (by subtracting the base) before calculating the actual offset address.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

For any array, the first element can always be found 0 elements from the start of the array. Thus, given an array of n elements, the valid range of subscripts must be 0 to n-1, never 1 to n.

Note that the array subscript operator [] is nothing more than sugar-coating for pointer arithmetic. An array is simply a block of memory containing one or more elements of a given type. Knowing the start address we can easily compute the offset address of any element knowing only its index within the array, and the first element is always offset 0 elements from the start of the array.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Array indices in C and C++ start from zero because that is the agreed language definition.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why array indexes starts from zero in c-language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the upper bound of an array whose size is 100?

The upper bound is the size minus 1 since VB starts with zero not one.


How do you obtain a single value from array?

That depends on the language you use, but I guess you're using C. So, here goes: int x[10] = {0}; //declare an array with ten indexes (0 thru to 9) printf("%i\n", x[0]); //print x[0], which should be zero.


Why array index always starts with zero?

Because that's how the language is defined. In C, C++, and many other languages, array indecies start with zero. This is by convention, and it also reflects the fact that, in C and C++, array-index syntax can also be expressed in pointer-offset syntax. The pointer locates the first element of the array, and the offset indicates how many elements to skip. If a is an array name, then the expression a[n] is completely identical to the expression *(a+n), and vice versa.


Index of an element in array?

An array is a group of related elements, with a common variable name. The index is a number that indicates the position of an element within an array: the 1st. element, the 2nd. element, etc. (many languages start counting at zero).


How do you start array list from 1?

Array indices are zero-based because the first element is at offset zero from the start of the array. There is no such thing as a one-based array in C. Some other languages do allow you to specify the base of an array, but converting a non-zero-based index to a zero-based index adds a runtime overhead to the implementation.


What do array subscripts always have?

Array subscripts always have a zero-based index. In languages that allow an n-based index, the index is simply offset by n elements, so the compiler subtracts n from the given index to obtain the zero-based index. Arrays are always zero-based because the first element of an array is found zero elements from the start of the array.


How do you read data from array?

Use the array suffix operator [] to access the individual elements of an array through a zero-based index.


Why numeric arrays do not end with a '0' in C programming language?

The value zero is a perfectly valid numeric array element in its own right. If we used the value zero to mark the end of a numeric array in the same way we use a null-terminator to mark the end of a string (a character array), we'd have no way of representing the value zero within the array itself. Thus when working with numeric arrays, we must keep track of the array length independently of the array.


Why array indexes starts from zero in c language?

An array is a pointer to a collection of memory locations in consecutive order, and an array's first element starts at the index's memory location. You can thus visualize it like this: ptr = (0x12345). ptr+0 = (0x12345). ptr[0] = (0x12345). ptr+1 = (0x12346). ptr[1] = (0x12346). This is technically a byte-based example, but the principle holds true regardless of data size if you assume all elements are byte aligned with the data element's size; you can then consider yourself to be accessing the "xth" element of a linear array of memory that is a number of units long (the total memory available to the program). Some languages start with "1" as the first index. Those languages either "waste" the 0th index as a matter of convenience, or silently adjust the index you access by one to match hardware expectations of indexing (e.g. the 10th index is the 11th physical array index).


Why the array is starting from zero?

By design; it makes the compiler's work easier. 1-based array's addressing-function: Address (array, index) = Address (array) + (index-1)*Elemsize(array) 0-based array's addressing-function: Address (array, index) = Address (array) + index*Elemsize (array)


What are sparse matrixes?

A sparse matrix is an array with more zero values than non-zero values.


What is an Array and to use Array in Action script?

Array is a ranged variable, using it is required by many actions.. like where you going to processing data from a file where there is n lines in a file and you want to get one line of it.. ex: if variable $line is an array of lines in a text file: $line[1] is second line of the file... just keep it in mind arrays starts from zero :) it means line 1 of the file will be accessed with $line[0];