title: Pointers and References - Simple CharArray Manipulation Program
body: <p>I'm trying to write a program in C++ which goes through a char array, replacing ch1 with ch2 as it goes along, the problem I am having is getting it to change the actual string which is inputed rather than return a new string. In C# you would use "ref" but I'm confused how to do this in c++, would you need a * or &amp;?</p>
  
  <p>The code below has lots of errors:</p>
  
  <p>(7): error C2664: 'strlen' : cannot convert parameter 1 from 'char' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast</p>
  
  <p>(7): error C2440: 'initializing' : cannot convert from 'char *' to 'char []' There are no conversions to array types, although there are conversions to references or pointers to arrays</p>
  
  <p>(11): error C2100: illegal indirection</p>
  
  <p>(17): error C2100: illegal indirection</p>
  
  <p>(24): error C2440: '=' : cannot convert from 'char []' to 'char' There is no context in which this conversion is possible</p>
  
  <p>(36): error C2664: 'replace' : cannot convert parameter 1 from 'char [17]' to 'char &amp;' </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cstring&gt;
  using namespace std;
  int replace(char &amp; str, char ch1, char ch2)
  {
  
      char newStr[]= new char[strlen(str)+1];
      int i=0;
      while(str!='\0')
      {
          if(*str==ch1)
          {
              newStr[i]=ch2;
          }
          else
          { 
              newStr[i]=*str;
          }
          str++;
          i++;
      }
  
      newStr[i]='\0';
      str = newStr;
  
      return 0;
  }
  
  int main()
  {
      char str[]="My name is Craig";
      char takeOut='C';
      char changeTo='a';
  
      cout &lt;&lt; "Before: " &lt;&lt; str &lt;&lt;endl;
      int changes=replace(str, takeOut, changeTo);
      cout &lt;&lt; "After: " &lt;&lt; str &lt;&lt; endl;
  
      cin.get();
      cin.get();
      return 0;
  }
  </code></pre>