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. -
#include <iostream.h>
-
-
void main()
-
-
{
-
-
void swap(int x, int y);
-
-
int first_num = 10;
-
-
int second_num = 50;
-
-
cout << "\nThe value of first_num is " << first_num << endl
-
-
<< "The value of second_num is " << second_num << endl;
-
-
swap(first_num, second_num);
-
-
cout << "\nThe value of first_num is now " << first_num << endl
-
-
<< "The value of second_num is now " << second_num <<
-
-
endl;
-
-
}
-
-
void swap(int x, int y)
-
-
{
-
-
int local_int;
-
-
local_int = x;
-
-
x = y;
-
-
y = local_int;
-
-
cout << "\nIn the function first_num is " << x << endl
-
-
<< "In the function second_num is " << y << endl;
-
-
}
-
2 1674
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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);
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;
}
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |