439,978 Members | 1,362 Online Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

# Function order.

 P: n/a 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....and also thank you all for making this so pleasurable. May 2 '06 #1
16 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 temporaryvariable.So if we have if( (a=b) == 0 )this is equivalent totemp = (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

 P: n/a 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

 P: n/a Robert Gamble wrote: Ian Collins wrote: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 temporaryvariable.So if we have if( (a=b) == 0 )this is equivalent totemp = (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; Oops, sorry I got that wrong. The parenthesis ensure the assignment precedes the comparison, otherwise the result would be a = (b==0). -- Ian Collins. May 2 '06 #11

 P: n/a On Tue, 02 May 2006 13:37:25 +1200, 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 youOK,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 temporaryvariable. The parentheses do not create a sequence point the way the term is used in the standard. Side effects may not yet be complete. So if we have if( (a=b) == 0 )this is equivalent totemp = (a=b)if( temp == 0 ) Remove del for email May 2 '06 #12

 P: n/a Ian Collins wrote: Robert Gamble wrote: Ian Collins wrote: 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; Oops, sorry I got that wrong. The parenthesis ensure the assignment precedes the comparison, otherwise the result would be a = (b==0). No, it does not force the assignment to take place before the comparison. It could work out what value will be assigned to a, compare that against 0, then do the actual assignment. -- Flash Gordon, living in interesting times. Web site - http://home.flash-gordon.me.uk/ comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc May 2 '06 #13

 P: n/a Flash Gordon wrote: Ian Collins wrote: Robert Gamble wrote: Ian Collins wrote: 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; Oops, sorry I got that wrong. The parenthesis ensure the assignment precedes the comparison, otherwise the result would be a = (b==0). No, it does not force the assignment to take place before the comparison. It could work out what value will be assigned to a, compare that against 0, then do the actual assignment. True, but the net result is the same, the parenthesis are required to ensure the value of b is assigned to a and the value assigned to a compared with 0. -- Ian Collins. May 2 '06 #14 