Hey all.
I'am using Teach Yourself C in 21 Days, and here is a sample code(p.214). The aim of the code is to show how to let the program know the size of the array, by setting the value of the last array member to 0. When the program finds a 0 in the array, it knows that the end of the array is reached.
Here is the code
Code:
/* Passing an array to a function. Alternative way. */
#include <stdio.h>
#define MAX 10
int array[MAX+1], count;
int largest(int num_array[]);
int main( void )
{
/* Input MAX values from the keyboard. */
for (count = 0; count < MAX; count++)
{
printf("Enter an integer value: ");
scanf("%d", &array[count]);
if ( array[count] == 0 )
count = MAX; /* will exit for loop */
}
array[MAX] = 0;
/* Call the function and display the return value. */
printf("\n\nLargest value = %d\n", largest(array));
return 0;
}
/* Function largest() returns the largest value */
/* in an integer array */
int largest(int num_array[])
{
int count, biggest = -12000;
for ( count = 0; num_array[count] != 0; count++)
{
if (num_array[count] > biggest)
biggest = num_array[count];
}
return biggest;
}
My question is, is the +1 in array[MAX+1] required at all?
Thanks in advance.