B 20: Post incrementers in arrays as function arguments

J. J. Farrell jjf@bcs.org.uk
Thu Aug 17 17:14:00 GMT 2000


> From: "Brian J Ball" <bjball@sep.com>
>> This code has produced 3 different results on 4 systems.

I don't know what language this code is supposed to be in, so I'll
assume it's C89.
> #include <stdio.h>
> int i;
> void do_increment(int array[]){
> for(i;i<10;){
> array[i++]=i;
> }
> }
>> void print_array(int a, int b, int c){
> printf("%i\t%i\t%i\n",a,b,c);
> }
>> main(void){
> int array[10];
> i=0;
> do_increment(array);
> i=0;
> /* This use of var++ in a function call is
> 	 unpredictable */
> print_array(array[i++],array[i++],array[i++]);
> }

Not only unpredictable but also undefined. The compiler can choose
to refuse to compile this code if it wishes, or it can produce a
program which behaves in some arbitrary way, or anything else it
feels like doing.
> The only workarround is to know how your system behaves or define temp
> variables.

Correct. The program you give is not valid C89. The best workaround
is to re-write it in valid C so its behaviour is predictable on all
C compilers.
> However, this works on some platforms and could become an error in porting
> software.

It works on all platforms, by definition, since the platform is
allowed to do anything it fancies when it compiles this code!
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


More information about the Cygwin mailing list

AltStyle によって変換されたページ (->オリジナル) /