Kashish Bagga
Kashish Bagga

Reputation: 11

pointer output code. I'm getting opposite of the answer. Please explain how is it happening

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

Upvotes: 0

Views: 544

Answers (2)

user12002570
user12002570

Reputation: 1

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

Upvotes: 0

moriaz
moriaz

Reputation: 326

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

Upvotes: 1

Related Questions