Using negative indexing with arrays and pointers?

In the task, it says:

Create a pointer to an int called ptr and have it point to the last element in array arr .

so I wrote (the code after the comment):

int main() {
  int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  char s[] = "Farmer Jack realized that big yellow quilts were expensive!!";

  // Code for Checkpoint 1 goes here.
  int* ptr = &arr[-1]; 
  

However, this does not pass the task and I get the error message:

Did you declare a pointer and point it at the last element in array arr ?

I have checked the solution. The solution gives the answer as:

int* ptr1 = &arr[9];

and when I printout the pointers:

 int* ptr = &arr[-1]; 
  int* ptr1 = &arr[9];
  printf("%p\n", ptr);
  printf("%p\n", ptr1);

I get this output:
0x7ffe3c5fc37c (ptr1)
0x7ffe3c5fc3a4 (ptr2)

Can anyone explain what the difference is / why the memory address is different when using negative indexing?

Many thanks in advance!

1 Like

In your output, arr[9] is at the location 0x7ffe3c5fc3a4

Since we have an array of integers, so

std::cout << sizeof(int);

gives an output of 4 i.e. each int in our array will take up 4 bytes (Whether an int will take up 2 bytes or 4 bytes or something else will depend upon the implementation being used. That is why I used sizeof to find out the size being used for int by this specific implementation).

Let us move back and find the location of the first element of the array arr[0]
If we subtract (9 times 4) = 36 bytes [36 in decimal is 0x24 in hex], then we will find the location of arr[0]

0x7ffe3c5fc3a4 - 0x24  = 0x7ffe3c5fc380

So, arr[0] is at location 0x7ffe3c5fc380
If we go back 4 bytes i.e. arr[-1], we get

0x7ffe3c5fc380 - 0x4 = 0x7ffe3c5fc37c

which is the other output mentioned in your post.


Use of negative index varies by language.
In Python/Ruby, we can use arr[-1] to find the last element.
In Java, negative index will give error.
In C/C++, negative index will not give us the last element. Instead arr[-1] in the above snippet posted by you will take us 4 bytes (the size of one element) back from arr[0]. We will go out of bounds of the array, but in C/C++ the responsibility is on the programmer to respect the bounds (In other languages such as Python, an index error will be thrown). In C/C++, we get undefined behavior. If you print out the value stored in arr[-1], you will most likely see some random number or an error or something else (hence the term undefined behavior).

Here are a few links which may be of interest:

1 Like