Author | C Swap Function | Scar0ptics Member

Posts: 229 Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13 Rank: Mad User | | 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);
}
Edited by Scar0ptics on 15-11-16 13:01 |
 |
Author | RE: Solution | Scar0ptics Member

Posts: 229 Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13 Rank: Mad User | | 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);
}
Edited by Scar0ptics on 15-11-16 13:06 |
 |
Author | RE: C++ Swap Function | skeet Member

Posts: 12 Location:
Joined: 26.01.16 Rank: HBH Guru | | 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 )
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++ |
 |
Author | RE: C Swap Function | Scar0ptics Member

Posts: 229 Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13 Rank: Mad User | | 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  )
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?
Edited by Scar0ptics on 16-11-16 01:38 |
 |
Author | RE: C Swap Function | Scar0ptics Member

Posts: 229 Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13 Rank: Mad User | | I am also using the linux compiler, so I am not so sure throwing anything out is a good idea, although I have not tried anything yet. I think it needs all that...
ex.) G++ main.c |
 |
Author | RE: C Swap Function | skeet Member

Posts: 12 Location:
Joined: 26.01.16 Rank: HBH Guru | | Scar0ptics wrote:
I am also using the linux compiler, so I am not so sure throwing anything out is a good idea, although I have not tried anything yet. I think it needs all that...
ex.) G++ main.c
use the -Wall for gcc and g++ |
 |
Author | RE: C Swap Function | Scar0ptics Member

Posts: 229 Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13 Rank: Mad User | | Ok, I can try that too. I'm sure I'll have the same output either way, but thanks for sharing. |
 |
|