Solved

why the original string is getting changed

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

820 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