Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C Swap Function


Scar0ptics's Avatar
Member
0 0

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);

}


Scar0ptics's Avatar
Member
0 0

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);

}


skeet's Avatar
Member
0 0

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++


Scar0ptics's Avatar
Member
0 0

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?


Scar0ptics's Avatar
Member
0 0

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


skeet's Avatar
Member
0 0

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++


Scar0ptics's Avatar
Member
0 0

Ok, I can try that too. I'm sure I'll have the same output either way, but thanks for sharing.