How to catch a index value outside array range?

Let's say there is an array[5] = {4, 5, 3, 6, 7}. What is the real value associated with array[8] since it is outside the domain of index? I want my code to stop looping the main code when it reaches the max limit of its index. Any advice on how to do that?

#define NUM_ENTRIES(ARRAY)      (sizeof(ARRAY) / sizeof(ARRAY[0]))

void loop()
{
    const int       array[] = { 4, 5, 3, 6, 7 };
    const size_t    len     = NUM_ENTRIES(array);
    static size_t   index   = 0;

    if ( index < len )
    {
        Serial.println(array[index++]);
    }

    // ... WHATEVER ELSE YOU WISH TO DO ...
}

EDIT: Or perhaps you meant something more like …

#define NUM_ENTRIES(ARRAY)      (sizeof(ARRAY) / sizeof(ARRAY[0]))

void loop()
{
    const int       array[] = { 4, 5, 3, 6, 7 };
    const size_t    len     = NUM_ENTRIES(array);
    static size_t   index   = 0;

    while ( index >= len )
    {	}

    Serial.println(array[index++]);

    // ... WHATEVER ELSE YOU WISH TO DO ...
}

sizeof() does a nice job:

for (int i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++)
{

}

You can also get fancy when using functions and use templates

// T is what type it is: byte, char, int...unsigned long \
   N is the number of indexes in the array

template<typename T, size_t N> 
void FooBar(T (&arr)[N] )
{
   // was type byte changed to size_t suggested by lloyddean
    for (size_t idx = 0; i < N; i++)
   {
      Serial.println(arr[ i ]);
   }
}

Not that it makes any difference for his described usage, but, given that ‘N’ is of type ‘size_t’ you should probably also have the loop local iterator variable ‘idx’ be of type ‘size_t’.

   for ( size_t idx = 0; i < N; i++ )

lloyddean:
Not that it makes any difference for his described usage, but, given that ‘N’ is of type ‘size_t’ you should probably also have the loop local iterator variable ‘idx’ be of type ‘size_t’.

   for ( size_t idx = 0; i < N; i++ )

I think the compiler would just type convert idx to unsigned if it’s needed.

Strictly speaking, you point out something that should be habitual!

shamilsaeed:
What is the real value associated with array[8] since it is outside the domain of index?

Whatever happens to be 4 bytes after the end of that array in memory. C does no bounds checking!

It may or may not remain constant as your program runs, depending on whether that specific memory location is used for something else.

Where is a '0' at last place. Text strings?

These aren't zero-terminated arrays.

A c string is a null (0) terminated char array - but that is only the case with arrays. The 0 is used to signify that the end of the string has been reached - if you forget that null, when you try to pass it to a function looking for a string, it will keep reading until it gets to a 0, which will likely involve reading things after the start of the array.