# Thread: Terminator in integer Array.

1. Hi, I'm Bill, a new member and a Unix guy.

I don't know why you need the terminator when you know how many numbers are in the array. Would you ever need to count numbers like this, say?

Code:
```counter = 0;
while (a[counter] != terminator)
counter++;```

2. > don't know why you need the terminator when you know how many numbers are in the array.
Because that's what the assignment said to use.

3. Originally Posted by Salem
> don't know why you need the terminator when you know how many numbers are in the array.
Because that's what the assignment said to use.
I know that, but I wonder purpose the terminator serves when there's already a way to tell whether you've reached the end of the list that the array stores. Why does the teacher want the students to use a terminator then? To speed up a sequential search, I add the target to the end of a list. Then I know that the list contains it when there's another instance of the target before the added one.

Code:
```bool contains(int list[], const int target, const int length)
{
register int place = 0;

list[length + 1] = target;
while (list[place] != target)
place++;
return place < length + 1;
}```

4. > Why does the teacher want the students to use a terminator then?
Maybe they're trying to teach how strlen() works.
Maybe they're trying to convey one of several methods of solving the same problem.

> I add the target to the end of a list.
As your your approach, what if your int list[] were const?
For that matter, how do you know there is even room to append something to the array (even temporarily).
Even if there is room, how do you know you're not trashing some user data.
Code:
```int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
if( contains(list,10,5) )  // trashes my data
if( contains(list,10,9) )  // out of bound access```
Principle of least astonishment - Wikipedia

And 'register' stopped being useful in the 1990's, when every half-decent compiler started implementing decent optimisation options.

5. Originally Posted by Salem
> Why does the teacher want the students to use a terminator then?
Maybe they're trying to teach how strlen() works.
Maybe they're trying to convey one of several methods of solving the same problem.

> I add the target to the end of a list.
As your your approach, what if your int list[] were const?
For that matter, how do you know there is even room to append something to the array (even temporarily).
Even if there is room, how do you know you're not trashing some user data.
Code:
```int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
if( contains(list,10,5) )  // trashes my data
if( contains(list,10,9) )  // out of bound access```
Principle of least astonishment - Wikipedia

And 'register' stopped being useful in the 1990's, when every half-decent compiler started implementing decent optimisation options.
If the array had been const, I couldn't have added an element to it. I know I assumed the array had room for terminator, the second instance of the target. I haven't tried to run that function. But I ignored whether anyone else would do that. I posted it only to show a way to use a terminator.

I use "const" partly because I'm a huge fan of purely functional programming in Haskell. Haskell is excellent, I think, partly because it prevents function side effects. Sadly, though, C is permissive enough to allow subtle unwanted ones.

What about the keyword "register?" Well, it still seemingly helps when I run gcc's C compiler.

6. Originally Posted by BillMcEnaney
I use "const" partly because I'm a huge fan of purely functional programming in Haskell. Haskell is excellent, I think, partly because it prevents function side effects.
The thing is that declaring those two parameters as const is only useful to the implementation of the function, i.e., to prevent an accidental modification of parameters that should not be modified, but for such a small function it arguably doesn't really help. To the caller, it makes no difference either way, whereas it would have made a difference for the first parameter, i.e., to assure the caller that contains will indeed merely return a result indicating whether the array contains the search item, without modifying the array... yet you omitted it precisely because you intended to do something that callers normally wouldn't expect from a function with such a job.

Originally Posted by BillMcEnaney
Sadly, though, C is permissive enough to allow subtle unwanted ones.
... like modifying the content of an array in a function named contains?

Originally Posted by BillMcEnaney
What about the keyword "register?" Well, it still seemingly helps when I run gcc's C compiler.
Maybe if you didn't enable optimisations at all. A quick check of a program that correctly uses your contains function shows that compiling with gcc 9.3.0 at -O1 or higher produces correspondingly identical assembly output with or without the register keyword, i.e., it doesn't help. Not even in the slightest.

7. Laserlight, thank you for pointing out my mistake. I should have declared the array as constant and found another way to stop the loop. Before writing the function, I should have thought carefully, too, because I want to write functional programs in C when I can. Even if my computer won't allocate registers for register variables, I'll still use the keyword "register" because I may need to compile my programs on platforms where that word will get registers allocated.

Sadly, although I'm a perfectionist, I'm sometimes impulsive. So I suppose I wrote my function impulsively.

Popular pages Recent additions