To make it into valid code, break it into three operations:
void swap(int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
On Jul 12, 3:25 pm, Dave <[email protected]> wrote:
> @Tendua: The statement *a ^= *b ^= *a ^= *b violates the sequence
> point rule, which says that results are undefined if a variable is
> assigned more than one value between sequence points.
>
> Dave
>
> On Jul 12, 3:15 pm, tendua <[email protected]> wrote:
>
> > # include <stdio.h>
>
> > void swap(int *a, int *b)
> > {
> > *a ^= *b ^= *a ^= *b;
> > }
>
> > int main()
> > {
> > int a=45, b= 56;
> > // a ^= b ^= a ^= b;
> > swap(&a,&b);
> > printf("%d %d",a,b);
> > }
>
> > This code gives output 0, 45
> > While if we uncomment the line in main function and don't use swap
> > function, we get correct value. Explain why the same line when used in
> > swap function gives such output.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.