Reputation: 23
In this code once f
is assigned some value of i
, for example from inside the loop after 3 iterations f=i=2
, then next time f
cannot be assigned the value again: the value of f
has to be rock solid to first assignment only.
for(i=0;i<N-1;i++)
{
if(array[i+1]>array[i]);
else if(array[i+1]<array[i])
{
f=i;s=i+1;}
else
{f=i;
s=i+1;}
}
Once f
is initialized to one value of i
it should not change. Is there any operator in C which helps? I thought of static
but it's absolutely the wrong choice.
Upvotes: 0
Views: 79
Reputation: 632
There is simply no way to do what you want in C. You cannot set a variable to any value and then prevent it from being set again in the future. The best you can do is to set it some some value you do not plan to use normally, like -1 or something, then check to see if it equals that before you set it. But you cannot prevent it from being set by some operator.
Upvotes: 1
Reputation: 522049
You could set f
equal to some placeholder value initially. Then, check for this placeholder and only make an assignment once:
int f = -1;
for (i=0; i < N-1; i++)
{
if (array[i+1] > array[i]);
else if (array[i+1] < array[i])
{
if (f == -1) f = i;
s = i+1;
}
else
{
if (f == -1) f = i;
s = i+1;
}
}
Upvotes: 3