Solved

why the original string is getting changed

Posted on 2004-10-12
1
193 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now