C Swap Function
2.) Create a Swap Function so that it correctly swaps three Integers and has the following output. Also include the call to the function in main. You must use pointers in your solution.
0,1,2 1,2,0
#include<stdio.h>
int main(int argc, char ** argv) {
int x = 0; int y = 1; int z = 2;
printf("%d,%d\n", x,y,z);
// Swap Function
printf("%d,%d\n", x,y,z);
}
0,1,2 1,2,0
#include<stdio.h>
void swap (int * _x, int * _y, int * _z)
int main(int argc, char ** argv) {
int x = 0; int y = 1; int z = 2;
printf("%d,%d,%d\n", x,y,z);
swap (&x,&y,&z);
void swap (int * _x, int * _y, int * _z)
int x = * _x; int *_x = *_y; int *_y = *_z; int _*z = x;
printf("%d,%d,%d\n", x,y,z);
}
lol what
Since it's int main() return a value for standards sake in linux generally return(0) for success or non-zero for failure or just use stdlib.h EXIT_SUCCESS or EXIT_FAILURE.
You aren't using or parsing command line arguments for the program so I (personally) would scrap (int argc, char ** argv) in main (I also like (int argc, char *argv[]) better even though they are equivalent :p )
Also printf() is only formatting/outputting two variables when you provide three add an extra %d
the pointers also do not look correct
anyway isn't this C not C++
skeet wrote: lol what
Since it's int main() return a value for standards sake in linux generally return(0) for success or non-zero for failure or just use stdlib.h EXIT_SUCCESS or EXIT_FAILURE.
You aren't using or parsing command line arguments for the program so I (personally) would scrap (int argc, char ** argv) in main (I also like (int argc, char *argv[]) better even though they are equivalent :p )
Also printf() is only formatting/outputting two variables when you provide three add an extra %d
the pointers also do not look correct
anyway isn't this C not C++
I'm almost certain that the pointers are correct and this is in "C", my bad. I forgot to add an additional "%d", thank you for telling me.
I will be posting a test review later on "C". I did not run this through a compiler before posting as I knew users would check and criticize my work and provide other solutions. That is what I wanted.
If the pointers are not correct, can you please explain why?