1

I'm passing an array in function argument and when I tried to access the last element using arr[-1] in the function's body I'm getting some garbage. Can someone explain me why is that so?

Here is the code:

#include<stdio.h>

using namespace std;

int binsearch(int nelement, int arr[],int maxsize)
{
    int low = 0;
    int high = maxsize;

    printf("%d",arr[-1]);
    return 0;       
}

void main()
{
    int arr[] = {1,2,3,4,5,6,7,8,9};
    int flag = 3;
    printf("%d\n",arr[8]);
    flag = binsearch(flag,arr,sizeof(arr));
}

And here is the output:

9
-858993460
3
  • Negative indicies are already discussed here: stackoverflow.com/questions/3473675/negative-array-indexes-in-c Commented Jan 22, 2013 at 17:46
  • 1
    What's up with that code? You're using namespaces, which is a C++ feature; and using <stdio.h>, which is C... You want to do C? Ditch the namespaces. Commented Jan 22, 2013 at 17:54
  • Unless this code is for an embedded system, it is not valid and will not compile in standard C nor C++, for several reasons. Commented Jan 22, 2013 at 19:13

3 Answers 3

3

arr[-1] does not access the last element. Conceptually, it access the space before the first element, but the behavior is not defined.

To access the last element, use arr[nelement-1] or arr[NumberOfElements-1], depending on what you mean by the last element. (Are you considering the array to contain only nelement elements, or do you want the last element in the entire allocated array? If the latter, you will need to know the number of elements in the array in order to calculate the index of the last one.)

Sign up to request clarification or add additional context in comments.

3 Comments

actually when I use python we usually do this to access the last element of array and when i read it into my school notes i found the same, that is why i got confused why it is showing me garbage value. Anyways thanks for clarifying it.
Using arr[maxsize-1] will run into undefined behaviour for sure as well, as depending on the size of an int maxsize is twice or fours or eights times as large as the array carries int elements.
@alk: Fixed; I mistook their maxsize for the maximum number of elements, rather than the array’s size in bytes.
2

Array indicies shall be unsigned. Using a negative value as index to an array provokes undefined behaviour.


As a side note:

This code won't pass the number of (int) elements down to the function:

int arr[] = {1,2,3,4,5,6,7,8,9};
...
flag = binsearch(flag, arr, sizeof(arr));

As the sizeof operator returns the number of bytes used by arr.

To pass the number of ints the array provides you might like to used the following construct:

flag = binsearch(flag, arr, sizeof(arr)/sizeof(arr[0]));

Comments

0

Since maxsize is sizeof(arr) and arr is an array of int - it is the (sizeof(int) * no_of_elements_of_arr)

it must be arr[(maxsize/sizeof(int)) - 1] in place of arr[-1]

arr[-1] is expected to print some garbage value

1 Comment

Using arr[maxsize-1] will run into undefined behaviour for sure as well, as depending on the size of an int maxsize is twice or fours or eights times as large as the array carries int elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.