Donate to us via Paypal!
Few are those who can see with their own eyes and hear with their own hearts. - Albert Einstein
Monday, March 01, 2021
Navigation
Home
 Find:
 Information:
Learn
Communicate
Submit
Shop
Challenges
 Exploit:
 Programming:
 Think:
 Track:
 Patch:
 Other:
 Need Help?
Other
Members Online
Total Online: 129
Guests Online: 127
Members Online: 2

Registered Members: 133766
Newest Member: denim6321d
Latest Articles

View Thread

HellBound Hackers | Computer General | Programming

Author

C Swap Function

Scar0ptics
Member



Posts: 229
Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13
Rank:
Mad User
Posted on 15-11-16 01:08
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
overlay-network.ddns.net
Author

RE: Solution

Scar0ptics
Member



Posts: 229
Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13
Rank:
Mad User
Posted on 15-11-16 01:14
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
overlay-network.ddns.net
Author

RE: C++ Swap Function

skeet
Member



Posts: 12
Location:
Joined: 26.01.16
Rank:
HBH Guru
Posted on 15-11-16 02:50
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 Pfft )

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
Posted on 15-11-16 12:57
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 Pfft )

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
overlay-network.ddns.net
Author

RE: C Swap Function

Scar0ptics
Member



Posts: 229
Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13
Rank:
Mad User
Posted on 15-11-16 16:37
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
overlay-network.ddns.net
Author

RE: C Swap Function

skeet
Member



Posts: 12
Location:
Joined: 26.01.16
Rank:
HBH Guru
Posted on 16-11-16 22:44
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
Posted on 17-11-16 02:49
Ok, I can try that too. I'm sure I'll have the same output either way, but thanks for sharing.
overlay-network.ddns.net