Solved

why the original string is getting changed

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

24 Experts available now in Live!

Get 1:1 Help Now