473,703 Members | 2,438 Online

# Function order.

May I ask the group the following: (Again, alas , from K&R)

This is part of a function:

while ( ( array1[i++] = array2 [k++]) != '\0' ); /* etc etc */
Is this the order that this is evaluated?

-> array2[k] is assigned to array1[i] ....???? the reason being it is
within parenthesis ???

-> array1[i] is tested for non-equality to '\0'

-> if TRUE array1[i] is incremented to array1[i+1]

-> if TRUE array2[k] is incremented to to array2[k+1]

-> if FALSE exit loop and neither k nor i is incrmented.

Thank you for your indulgence....a nd also thank you all for making this
so pleasurable.

May 2 '06 #1
16 2445
mdh wrote:
May I ask the group the following: (Again, alas , from K&R)

This is part of a function:

while ( ( array1[i++] = array2 [k++]) != '\0' ); /* etc etc */
Is this the order that this is evaluated?

-> array2[k] is assigned to array1[i] ....???? the reason being it is
within parenthesis ???
Correct.

Also both i and k are incremented.

The expression could be written as

do
{
array1[i] = array2[k];
++i;
++k;
} while( array1[i-1] != '\0' )

-> array1[i] is tested for non-equality to '\0'

-> if TRUE array1[i] is incremented to array1[i+1]
No, i is incremented in the copy.
-> if TRUE array2[k] is incremented to to array2[k+1]
No, k is incremented in the copy.
-> if FALSE exit loop and neither k nor i is incrmented.

No, i and k will have been incremented.

--
Ian Collins.
May 2 '06 #2

Ian Collins wrote:
-> array1[i] is tested for non-equality to '\0'

Ian,

am I correct in that array1 NOT array2 is tested for inequality to
'\0'?

Thanks.

May 2 '06 #3
mdh wrote:
Ian Collins wrote:

-> array1[i] is tested for non-equality to '\0'

Ian,

am I correct in that array1 NOT array2 is tested for inequality to
'\0'?

It's the result of the assignment operation that is tested.

--
Ian Collins.
May 2 '06 #4
Ian Collins wrote:
mdh wrote:
Ian Collins wrote:
-> array1[i] is tested for non-equality to '\0'

Ian,

am I correct in that array1 NOT array2 is tested for inequality to
'\0'?

It's the result of the assignment operation that is tested.

Which incidentally is why we have the common assignment rather than
comparison bug,

if( i = 42 )

--
Ian Collins.
May 2 '06 #5

Ian Collins wrote:
It's the result of the assignment operation that is tested.

Ian, this is all rather new to me. Naively I thought that the array
was compared to '\0' so I am not quite sure by what you mean with
"result of the assignment operation". Is it possible to make your
explanation a little more concrete. Thank you

May 2 '06 #6
mdh wrote:
Ian Collins wrote:

It's the result of the assignment operation that is tested.

Ian, this is all rather new to me. Naively I thought that the array
was compared to '\0' so I am not quite sure by what you mean with
"result of the assignment operation". Is it possible to make your
explanation a little more concrete. Thank you

OK,

I might get some terms wrong, but others will correct me if I do...

(a = 42) is an assignment expression, the result of the expression is 42
.. The parenthesis ccreate a sequence point, like a hidden temporary
variable.

So if we have if( (a=b) == 0 )

this is equivalent to

temp = (a=b)
if( temp == 0 )

--
Ian Collins.
May 2 '06 #7

Ian Collins wrote:

(a = 42) is an assignment expression, the result of the expression is 42
. The parenthesis ccreate a sequence point, like a hidden temporary
variable.

So if we have if( (a=b) == 0 )

this is equivalent to

temp = (a=b)
if( temp == 0 )

Thanks Ian....I "think" I understand :-)

May 2 '06 #8
mdh wrote:
Ian Collins wrote:
(a = 42) is an assignment expression, the result of the expression is 42
. The parenthesis ccreate a sequence point, like a hidden temporary
variable.

So if we have if( (a=b) == 0 )

this is equivalent to

temp = (a=b)
if( temp == 0 )

Thanks Ian....I "think" I understand :-)

Once you understand the concept of an assignment yielding a result, the
fog clears...

--
Ian Collins.
May 2 '06 #9
Ian Collins wrote:
mdh wrote:
Ian Collins wrote:

It's the result of the assignment operation that is tested.
Ian, this is all rather new to me. Naively I thought that the array
was compared to '\0' so I am not quite sure by what you mean with
"result of the assignment operation". Is it possible to make your
explanation a little more concrete. Thank you

OK,

I might get some terms wrong, but others will correct me if I do...

(a = 42) is an assignment expression, the result of the expression is 42.

Correct.
The parenthesis ccreate a sequence point, like a hidden temporary
variable.

So if we have if( (a=b) == 0 )

this is equivalent to

temp = (a=b)
if( temp == 0 )

Absolutely not. Parenthesis do not introduce sequence points, the
following is undefined:
a = (a++);

where as this is not:

temp = a++;
a = temp;

Robert Gamble

May 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.