473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointers and Swap

1
Ok, so I've successfully written my swap application but now i'm trying to rewrite it so i'm using pointers to pass the address of the argument to the function. I'm going to have the swapping performed in the main function after calling the swap function.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2.  
  3. void main()
  4.  
  5. {
  6.  
  7. void swap(int x, int y);
  8.  
  9. int first_num = 10;
  10.  
  11. int second_num = 50;
  12.  
  13. cout << "\nThe value of first_num is " << first_num << endl
  14.  
  15. << "The value of second_num is " << second_num << endl;
  16.  
  17. swap(first_num, second_num);
  18.  
  19. cout << "\nThe value of first_num is now " << first_num << endl
  20.  
  21. << "The value of second_num is now " << second_num <<
  22.  
  23. endl;
  24.  
  25. }
  26.  
  27. void swap(int x, int y)
  28.  
  29. {
  30.  
  31. int local_int;
  32.  
  33. local_int = x;
  34.  
  35. x = y;
  36.  
  37. y = local_int;
  38.  
  39. cout << "\nIn the function first_num is " << x << endl
  40.  
  41. << "In the function second_num is " << y << endl;
  42.  
  43. }
  44.  
Nov 9 '08 #1
2 1674
boxfish
469 Expert 256MB
The code you posted does not compile. If you want help with compiler errors you are getting, please post them here. Once you have this code working, what is your question about pointers? If you have specific questions about pointers or code that you have written, you can post them here, but what you have posted is not an answerable question.
Thanks.
Nov 10 '08 #2
arnaudk
424 256MB
Ok, so I've successfully written my swap application but now i'm trying to rewrite it so i'm using pointers to pass the address of the argument to the function.
So then your function will look like
void swap(int* a, int* b);
and it dereferences the pointers and swaps the integers they point to.
EDIT: Sorry, boxfish is right, upon looking at your code, I also see some glaring errors which you'd better fix first. Even when you get it to compile, your swap function won't work. You need to pass pointers or references to your swap function, don't call by value as you'll create local variables x and y in your swap function which will have no bearing on first_num and second_num in main. Also, it's <iostream> not <iostream.h> and declare your swap function outside the body of main() and main() returns int not void and etc.
Nov 10 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: ex_ottoyuhr | last post by:
I have a situation more or less as follows, and it's causing me no end of trouble; I'd appreciate anyone's advice on the matter... Given these classes: class BedrockCitizen { ... }; class...
4
by: ballpointpenthief | last post by:
I've put off posting this for a few days but this is still giving me grief: I just starting learning standard C and I plan to learn properly. I *think* I understand pointers, however after...
1
by: bob | last post by:
let's say you want a function to swap pointers. is this how you take two pointers by reference: void swap(char* &p1, char* &p2);
7
by: Eli Luong | last post by:
Hi, Sorry if I accidentally posted a blank post before. But, I was wondering, I'm practicing with pointers right now, and wanted to know if I am using it correctly in the following code. By...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
16
by: ajinkya.coep | last post by:
What's wrong with this program? If you were to fix it, what would the intended output be? void swap(char *str, int index1, int index2) { char tmp = str; str = str; str = tmp; } int...
1
by: Puddle | last post by:
Hi everyone, I'd just like to preface by saying any input or direction in this would be very much appreciated; i've been working on this endlessly and can't figure out what's going wrong. I'm...
2
by: kpdp | last post by:
Hey. Can anyone tell me why we need pointers when swapping varibles such as: void swap (int *x, int *y) { int temp; temp = *x; *x =*y; *y = temp; }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.