Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

It's about offsets. You have an address, which points to the location in memory where the array begins. Then to access any element, you multiply the array index by the size of the element and add it to the starting address, to find the address for that element.

The first element is at the starting point, so you multiply the size of the element by zero to get zero which is what you add to the starting address to find the location of the first element.

The convention spread because programmers started working in very low-level languages where memory addresses were directly manipulated and in most cases building up from there, maintaining the same convention at each step so that they wouldn't have to relearn or be prone to mistakes when switching between conventions. It's still important to understand how this addressing works especially when working with lower-level languages. I agree this can be a stumbling block for people who are first learning to program in a higher-level language.

The Wikipedia article on this topic also cites a common machine instruction used when working "backwards" and detecting the end of a loop, namely "decrement and jump if zero."

An exception: MATLAB and some other languagesand some other languages bucked the trend and went with an index starting at 1, apparently under the impression that it would be a first programming language for a lot of their target users and that for those folks, starting with 1 makes more intuitive sense. This causes some frustrations for the (relatively small subset of?) programmers who frequently switch between programming languages that start counting at different values.

It's about offsets. You have an address, which points to the location in memory where the array begins. Then to access any element, you multiply the array index by the size of the element and add it to the starting address, to find the address for that element.

The first element is at the starting point, so you multiply the size of the element by zero to get zero which is what you add to the starting address to find the location of the first element.

The convention spread because programmers started working in very low-level languages where memory addresses were directly manipulated and in most cases building up from there, maintaining the same convention at each step so that they wouldn't have to relearn or be prone to mistakes when switching between conventions. It's still important to understand how this addressing works especially when working with lower-level languages. I agree this can be a stumbling block for people who are first learning to program in a higher-level language.

The Wikipedia article on this topic also cites a common machine instruction used when working "backwards" and detecting the end of a loop, namely "decrement and jump if zero."

An exception: MATLAB and some other languages bucked the trend and went with an index starting at 1, apparently under the impression that it would be a first programming language for a lot of their target users and that for those folks, starting with 1 makes more intuitive sense. This causes some frustrations for the (relatively small subset of?) programmers who frequently switch between programming languages that start counting at different values.

It's about offsets. You have an address, which points to the location in memory where the array begins. Then to access any element, you multiply the array index by the size of the element and add it to the starting address, to find the address for that element.

The first element is at the starting point, so you multiply the size of the element by zero to get zero which is what you add to the starting address to find the location of the first element.

The convention spread because programmers started working in very low-level languages where memory addresses were directly manipulated and in most cases building up from there, maintaining the same convention at each step so that they wouldn't have to relearn or be prone to mistakes when switching between conventions. It's still important to understand how this addressing works especially when working with lower-level languages. I agree this can be a stumbling block for people who are first learning to program in a higher-level language.

The Wikipedia article on this topic also cites a common machine instruction used when working "backwards" and detecting the end of a loop, namely "decrement and jump if zero."

An exception: MATLAB and some other languages bucked the trend and went with an index starting at 1, apparently under the impression that it would be a first programming language for a lot of their target users and that for those folks, starting with 1 makes more intuitive sense. This causes some frustrations for the (relatively small subset of?) programmers who frequently switch between programming languages that start counting at different values.

Post Migrated Here from stackoverflow.com (revisions)
Source Link
WBT
  • 221
  • 1
  • 4
  • 8

It's about offsets. You have an address, which points to the location in memory where the array begins. Then to access any element, you multiply the array index by the size of the element and add it to the starting address, to find the address for that element.

The first element is at the starting point, so you multiply the size of the element by zero to get zero which is what you add to the starting address to find the location of the first element.

The convention spread because programmers started working in very low-level languages where memory addresses were directly manipulated and in most cases building up from there, maintaining the same convention at each step so that they wouldn't have to relearn or be prone to mistakes when switching between conventions. It's still important to understand how this addressing works especially when working with lower-level languages. I agree this can be a stumbling block for people who are first learning to program in a higher-level language.

The Wikipedia article on this topic also cites a common machine instruction used when working "backwards" and detecting the end of a loop, namely "decrement and jump if zero."

An exception: MATLAB and some other languages bucked the trend and went with an index starting at 1, apparently under the impression that it would be a first programming language for a lot of their target users and that for those folks, starting with 1 makes more intuitive sense. This causes some frustrations for the (relatively small subset of?) programmers who frequently switch between programming languages that start counting at different values.