#include<iostream>
using namespace std;
//swap function
void swap (char *x, char *y)
{
char *t = x;
x = y;
y = t;
}
int main()
{
char *x = "geeksquiz";
char *y = "geeksforgeeks";
char *t;
swap(x, y);
cout<<x << " "<<y;
t = x;
x = y;
y = t;
cout<<" "<<x<< " "<<y;
return 0;
}

- 25,259
- 5
- 41
- 83

- 11
- 1
-
After dry run, I am getting the output as "geeksforgeeks geeksquiz geeksquiz geeksforgeek" whereas the output is "geeksquiz geeksforgeeks geeksforgeeks geeksquiz" – Kashish Bagga Mar 12 '22 at 09:02
-
Your swap() function is not actually swapping anything, since it takes its input parameters by value, thus copies of the inputs are made, and you are swapping the copies around, not the originals – Remy Lebeau Mar 12 '22 at 09:03
-
Remember that function arguments in C++ are passed *by value*, which means the values in the call are copied into the functions local argument variables. Assigning to a local variable only affect the local variable itself and not the original value. You need to pass the arguments *by reference*. – Some programmer dude Mar 12 '22 at 09:03
-
4Geeksforgeeks is a garbage collection. If you want to learn C++, prefer a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also see: [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Mar 12 '22 at 09:04
-
Also, all literal strings in C++ are really *constant* arrays of characters (including the null-terminator). So all pointers to such string literals needs to be constant as well: `const char* x = "geeksquiz";`. – Some programmer dude Mar 12 '22 at 09:04
-
4Both of my comments (about passing by reference and about literal strings being constant) should be taught in any decent book or class. So instead of using bad online tutorials (like geek for geeks, or so-called "competition" sites) invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take proper classes, to learn C++ and programming and computer science properly. – Some programmer dude Mar 12 '22 at 09:07
-
Thannk you so much. Got that :) – Kashish Bagga Mar 12 '22 at 09:18
2 Answers
In the swap function that you have implemented, you are modifying local x and y, which has no effect on the x and y in the main function. There are two solutions for that.
1- Adding another level of pointer to the parameters:
void swap_pointer (char **x, char **y)
{
char *t = *x;
*x = *y;
*y = t;
}
and then calling the function as swap_pointer(&x, &y);
2- changing the function parameters to reference types:
void swap_reference (char *&x, char *&y)
{
char *t = x;
x = y;
y = t;
}
and then calling the function as swap_reference(x, y);
There are 2 main problems with your code both of which are explained below:
Problem 1
In C++, we cannot have char*
pointing to a string literal. This means, the following statements in your program are invalid:
char *x = "geeksquiz"; //INVALID
char *y = "geeksforgeeks"; //INVALID
Solution to Problem 1
To solve this, we have to add a low-level const to the pointers as shown below:
//--vvvvv---------------------------------->const added here
const char *x = "geeksquiz";
//--vvvvv---------------------------------->const added here
const char *y = "geeksforgeeks";
Problem 2
In addition to the above problem, the function named swap
in your program takes argument by value. This means, whatever changes you make to those arguments inside the function will not be reflected back on the original passed arguments. This in turn means that you're actually performing a swap on the copies and not on the original arguments which explains why you don't get your expected result.
Solution to Problem 2
You can solve this by passing the argument by reference which can be done by making the parameter to be reference to const char*
as shown below:
#include<iostream>
//---------vvvvv---------vvvvv----------->const added here
void swap (const char *&x,const char *&y)
//---------------------^--------------^--> & added here
{
//vvvvv---------------------------------->const added here
const char *t = x;
x = y;
y = t;
}
int main()
{
//-vvvvv------------------------------->const added here
const char *x = "geeksquiz";
//-vvvvv------------------------------->const added here
const char *y = "geeksforgeeks";
swap(x, y); //pass arguments by reference
std::cout<<x << " "<<y;
return 0;
}
Alternative solution
Note that you can also use std::string
instead of using pointers as shown below:
#include<iostream>
#include <string>
void swap (std::string &x,std::string &y)
{
std::string temp = x;
x = y;
y = temp;
}
int main()
{
std::string x = "geeksquiz";
std::string y = "geeksforgeeks";
swap(x, y); //pass arguments by reference
std::cout<<x << " "<<y;
}

- 36,170
- 5
- 26
- 60