Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I came upon a c++ book informing me on how arrays access their element. it said:

"when asked to access element at index N, the compiler uses the memory address of the first element (positioned at index zero) as the starting point and then skips N elements by adding the offset computed as N * sizeof(element) to reach the address containing the (N + 1)th element".

I didn't understand this because aren't you supposed to get to element N and not element N + 1?

What I have tried:

I have tried to research about it but I can't find any information.
Posted
Updated 6-Apr-21 20:50pm

You might be able to see why the "element at index N" is the (N+1)th element by looking at this list :
1st : [0]
2nd : [1]
3rd : [2]
4th : [3]
...
(N+1)th : [N]
 
Share this answer
 
Comments
iwanttoaskquestions 7-Apr-21 2:30am    
Im sorry but i still don't see why
CPallini 7-Apr-21 2:53am    
5.
Because the arrays, in C/C++ programming languages, are 0-based: the 'first item' is the 'item 0', the 'second item' is 'item 1' and so on:
C++
int a[3];
a[0] = 15; // assign 15 to the first item of the array, that is the item 0.
a[1] = 12; // assign 12 to the second item of the array, that is the item 1.
a[2] = 42; // assign 42 to the third item of the array, that is item 2.
// note there is NO a[3] (because it would be the fourth item)


The following program,
C++
#include <iostream>
using namespace std;

int main()
{

  int a[3];
  a[0] = 15; 
  a[1] = 12; 
  a[2] = 42; 
  
  cout << "the address of a is: " << a << endl;

  for ( int n = 0; n < 3; ++n)
  {
    cout << "the address of the " << (n+1) << "-th item, that is &a[" << n << "] is: " << &a[n] << endl;
  }
}
on my Linux box, outputs
the address of a is: 0x7ffd1275d54c                                                                                                                        
the address of the 1-th item, that is &a[0] is: 0x7ffd1275d54c                                                                                             
the address of the 2-th item, that is &a[1] is: 0x7ffd1275d550                                                                                             
the address of the 3-th item, that is &a[2] is: 0x7ffd1275d554    
 
Share this answer
 
v2
Quote:
Im sorry but i still don't see why

Think about it: you were born on October 23rd 2000, so your first birthday was October 23rd 2001, your second on October 23rd 2002, and so on. The year is indexed from 2000, the birthday numbers run 1st, 2nd, 3rd, and so on.

This called zero-based and one-based indexing. "Human values" such as age run with a "one-based index" because we never say "my baby is zero years old", or "he is 0 months and five days", computers run with "zero-based" indexes.

So the first element (human term) of an array is at index zero, the second is at index 1, and so on.
C#
string[] human = new string[] {"1st", "2nd", "3rd"};
for (int i = 0; i < human.Length; i++)
   {
   Console.WriteLine($"The {human[i]} element is at index {i}");
   }
So the element at index N will be the "N + 1"th element.

Does that make sense?
 
Share this answer
 
Comments
iwanttoaskquestions 7-Apr-21 3:16am    
ahh i see
iwanttoaskquestions 7-Apr-21 3:16am    
thanks
OriginalGriff 7-Apr-21 4:14am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900