# need help with analyzing some code

• 01-30-2009
happyclown
need help with analyzing some code
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?

• 01-30-2009
laserlight
Quote:

Originally Posted by happyclown
My question is, is the +1 in array[MAX+1] required at all?

Yes, if you want up to MAX elements and yet have a special element be designated as the terminator. This is akin to how null terminated strings work.

By the way, both the array and count global variables should be local variables in main().
• 01-30-2009
auralius
Quote:

Originally Posted by laserlight
Yes, if you want up to MAX elements and yet have a special element be designated as the terminator. This is akin to how null terminated strings work.

Hello, laserlight, but I think it is not string.
It is array of integer.
I am sure it is not necessary...
• 01-30-2009
matsp
Quote:

Originally Posted by auralius
Hello, laserlight, but I think it is not string.
It is array of integer.
I am sure it is not necessary...

Yes, but it's a ZERO-terminated array of integers - a string is a zero-terminated array of characters - the concept is very similar.

--
Mats
• 01-30-2009
vart
to write this
array[MAX] = 0;
you need an array of at least MAX+1 elements
• 01-30-2009
auralius
but it's not to be printed out...
it only prints a single integer...

Quote:

to write this
array[MAX] = 0;
you need an array of at least MAX+1 elements
I definitely agree... but trust me... it won't cause any error when you run...
better omit it.. right?
• 01-30-2009
happyclown
Quote:

Originally Posted by auralius

I definitely agree... but trust me... it won't cause any error when you run...
better omit it.. right?

I tested the program without the +1, and it seems to run ok.
• 01-30-2009
matsp
Quote:

Originally Posted by happyclown
I tested the program without the +1, and it seems to run ok.

That's because it's undefined behaviour. I can cross Waterloo road when the red-man [don't walk] is showing without looking and get across the road - once or twice. But if I constantly cross roads without looking at the signs, I will eventually get run over by a big red bus.

Did you check the value of count after you set array[MAX] = 0?

--
Mats