0
#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; } 
7
  • After dry run, I am getting the output as "geeksforgeeks geeksquiz geeksquiz geeksforgeek" whereas the output is "geeksquiz geeksforgeeks geeksforgeeks geeksquiz" Commented Mar 12, 2022 at 9: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 Commented Mar 12, 2022 at 9: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. Commented Mar 12, 2022 at 9:03
  • 4
    Geeksforgeeks is a garbage collection. If you want to learn C++, prefer a good book. Also see: Why is using namespace std; considered bad practice? Commented Mar 12, 2022 at 9:04
  • 4
    Both 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 and take proper classes, to learn C++ and programming and computer science properly. Commented Mar 12, 2022 at 9:07

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Working demo

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

Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.