I do remember the time when I first encountered the same problem ( while I was trying to build a big number library using int arrays ), and eventually I figured out pretty much the same as what other answers say that technically '\0' and 0 have the same value.
Now here are 2 ways that I used to overcome this problem and these are only applicable under certain conditions
Condition : When all your input elements are positive
Now since all your input elements are positive, you can mark the end of the array by inserting a negative number
Typically, I use -1, this way :
int a[] = {1, 2, 3, 4, -1}
for(int index = 0; a[index] != -1; index++)
{
//use the array element a[index] for desired purpose!
}
Instead you can enter any negative number and do it this way
for(int index = 0; a[index] >= 0; index++)
{
//use the array element a[index] for desired purpose!
}
Condition : When all your elements are bound within a certain range
You might have got the idea by now :), lets say that all your elements belong to the range [-100,100]
you can insert any number above or below the bounds of the range to mark the end... so in the above case I can mark the end by entering a number < -100 and >100.
And you can iterate the loop this way :
for(int index = 0; (a[index] > -100) && (a[index] < 100); index++)
{
//use the array element a[index] for desired purpose!
}
Generalizing both the cases, just place a value at the end of array which you know for sure is not equal to an array element
for(int index = 0; a[index] != value_not_in_array; index++)
{
//use the array element a[index] for desired purpose!
}
So, now under Case 1, your while loop condition can be either of the following :
while(sums[t] != -1) //typically ended with `-1`
//(or)
while (sums[t] >= 0) //ended with any negative number
And under Case 2 :
while ((sums[t] >min_range) && (sums[t] < max_range)) // when elements are bound within a range
Or more generally :
while( sums[t] != value_not_in_array )
The underlying fact of both the cases is that I'm finding out a
potential replacement for terminating '\0' character.
Hope this helps, happy coding ;)
'\0'is anintwith the value0. There is no type difference. They are indistinguishable.null int, please see other comments.