Solved

why the original string is getting changed

Posted on 2004-10-12
1
200 Views
Last Modified: 2010-04-01
In the following code , I wanted to print the reverse of a string which was to be stored in an another string without affecting the original string. I made the following function. But it is affecting the original string also. Please let me know what's wrong
____________________________________________________
#include "iostream"
#include "string"

using namespace std;


char * reverse (char b[]);

void main()

{

      char a1[]="howyoudoing";
      
      char *game=reverse(a1);

      cout << game <<endl;

      cout << a1 <<endl;

}


char * reverse (char b[])

{

      int len=strlen(b);

      char *temp=new char[len];

      temp=b;

      for (int i=0; i<len/2;i++)
      {
      
            char x=temp[i];
            temp[i]=temp[len-i-1];
            temp[len-i-1]=x;
      }

      

      return temp;

}
0
Comment
Question by:anshuma
1 Comment
 
LVL 2

Accepted Solution

by:
bcsonka earned 125 total points
ID: 12294084
Hi,

In C++, when calling a function or procedure that contains an array as a parameter, the array is always passed by reference, not by value.  In the reverse() function, you are assigning the pointer to temp to the same memory location as that pointed to by b, and b is pointing to the same memory location as the original string, a1.  Thus when modifying temp, you are also modifying a1.

Instead of temp=b; in reverse(), try strcpy(temp, b); instead.

I hope that helps.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question