Vonti
Vonti

Reputation: 415

How to check if current index is last index in array?

How do I say?

for (i = 0; i < arraySize; i++)
{
    if (someArray[i + 1] !NULL)
    {
        //do this
    }
    else
    {
        //do something else
    }
}

In other words, I want to check if the current index is the last index in the array. The current code is my best guess but it isn't working.

EDIT: It is an integer array and i less than arraySize not 0

EDIT 2: The code inside my for loop is more complicated than the example above but I can't paste all the code as it is top secret atm. I have many nested if statements. if(i==arraySize-1) helped me to solve the problem. Thanks.

Upvotes: 0

Views: 2852

Answers (2)

Sebi2020
Sebi2020

Reputation: 2150

Size of Array:

int array[20], i;
// ...
// fill array
// ...
int sz = sizeof(array)/sizeof(int);
for(i=0; i < sz;i++) {
if(array[i] != 0x0) {
// do something
}
}

Upvotes: 0

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

Following line will give you count of items in your array:

count  = sizeof (intMyArray)/sizeof(intMyArray[0]);

if you know arraySize then:

if(i==arraySize-1){
  //last element of array
}

Upvotes: 3

Related Questions