Reputation: 27
I have a doubt that why 3 variables wstart = wend = start; are used to initialise the loop.if the value of any of these variable changes will it change the value of both variable ???
void reverseletter(char str[], int start, int end) {
int wstart, wend;
for (wstart = wend = start; wend < end; wend++) {
if (str[wend] == ' ')
continue;
// Checking the number of words
// present in string to reverse
while (str[wend] != ' ' && wend <= end)
wend++;
wend--;
//Reverse the letter
//of the words
reverse(str, wstart, wend);
}
}
Upvotes: 0
Views: 76
Reputation: 50776
An assignement like b = c
is itself an expression whose value is the value of b
after the assignment.
Therefore
a = b = c;
can be seen as
a = (b = c);
which is equivalent to:
b = c;
a = b;
All three variables a,b and c stay totally independent of each other.
You can demonstrate this with following snippet:
int a = 1;
int b = 2;
printf("%d\n", a);
printf("%d\n", a = b);
printf("%d\n", a);
Output
1
2
2
Upvotes: 1
Reputation: 20244
wstart = wend = start
is the same as
wend = start;
wstart = start;
if the value of any of these variable changes will it change the value of both variable ?
No, changing one variable won't affect the others, all 3 are independent variables.
Upvotes: 3
Reputation: 86651
This snippet
wstart = wend = start
takes advantage of the fact that, in C, assignment is an expression that returns a value. What happens is that wend = start
assigns the value of start
to wend
and "returns" it. This "return" value is assigned to wstart
. Thus the overall effect is to assign start
to both wend
and wstart
.
It's a style I've seen before but I don't personally like it. I would prefer:
for (wstart = start, wend = start; wend < end; wend++)
which does the same thing but in a clearer way IMO.
Upvotes: 0
Reputation: 24895
If you change one of the variables, it will not change the other. All of them do not reference to the same memory location.
int wstart, wend;
for (wstart = wend = start; wend < end; wend++) {
wstart
, wend
and start
, each will have their copy of the values and changing one will not change others.
Upvotes: 0