This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: B 20: Post incrementers in arrays as function arguments


> 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]