Solved

why the original string is getting changed

Posted on 2004-10-12
1
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

739 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