title: Changing the value of a int variable through pointers passed as arguments?
body: <p>I want to modify values of some variables of a particular class by accessing address of these variables from another different class through a function. So, to access this address I try to pass pointers-to-variables as arguments to a function, where these pointers-to-variables will be set with the address of the variables. To learn how to do it, I'm trying to mimic in a simple program.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;    
  
  using namespace std;
  int numberA = 100;
  int numberB = 200;
  
  void referenceSetter(int *a, int *b)
  {
      *a = numberA;
      *b = numberB;
  }
  
  void numberOutput()
  {
      cout &lt;&lt; "A = " &lt;&lt; numberA &lt;&lt; endl;
      cout &lt;&lt; "B = " &lt;&lt; numberB &lt;&lt; endl;
  }
  
  int main() {
  
      int *testA = 0; 
      int *testB = 0;
      referenceSetter(testA, testB);
  
      *testA = 30;
      *testB = 40;
      numberOutput();
  
      return 0;
  }
  </code></pre>
  
  <p>As you could see I declare <code>numberA</code> and <code>numberB</code> as global variables and set their values. The I try to get the address of these two variables through the function <code>referenceSetter</code> function and then after that I try to modify the values in those variables using the references. Apparently, I'm doing something wrong which leads to to have <code>Unhandled Exception</code> error exactly when I try to modify the values and try to set them as 30 and 40 resepectively. </p>
  
  <p>Alternatively I tried the following approach:</p>
  
  <pre><code>#include &lt;iostream&gt;    
  
      using namespace std;
      int numberA = 100;
      int numberB = 200;
  
      void referenceSetter(int *a, int *b)
      {
          a = &amp;numberA;
          b = &amp;numberB;
      }
  
      void numberOutput()
      {
          cout &lt;&lt; "A = " &lt;&lt; numberA &lt;&lt; endl;
          cout &lt;&lt; "B = " &lt;&lt; numberB &lt;&lt; endl;
      }
  
      int main() {
  
          int *testA; 
          int *testB;
          referenceSetter(testA, testB);
  
          *testA = 30;
          *testB = 40;
          numberOutput();
  
          return 0;
      }
  </code></pre>
  
  <p>But this approach throws up the error <code>uninitialized local variables testA and testB</code>. Do I have to initialize pointers too?</p>
  
  <p>Please help me find my mistake. Thanks.</p>