title: Passing the address from an element of an array of pointers to set another pointer address
body: <p>I've tried searching through responses, but examples unfortunately seem too complex for me to understand. Most fundamentally I want to know how I can pass and change the addresses of an array of pointers. </p>
  
  <p>Example:</p>
  
  <pre><code>    class Foo {
        M** arryM;
        ...
        arryM = new M*[10];
        ...
        void Foo::replace(int ind, M &amp;newm) {
          // Q1: which one correct to change the address of an element to a new object's address?
          arryM[ind] = newm; // this correct to pass and reset a new address for pointer?
          arryM[ind] = &amp;newm; // or is this correct, or does this just set the object pointed by array element instead of the array element's address?
          *arryM[ind] = newm; // or do I need to dereference the pointer rep by array element first?
          &amp;arryM[ind] = &amp;newm; // or is this correct??
        }
        ...
        // Q2: is it easier to return a pointer or address for this function?
        M &amp; Foo::getM(int ind) {
          M *out;
          // Q3: which one of following correct to get correct address value to return?
          out = arryM[ind]; // get address to M obj pointed by pointer array elem
          out = *arryM[ind]; // is "arryM[ind]" == "*ptr"?? if so how to get to "ptr"?
          out = &amp;arryM[ind]; // is this way to get to "ptr" so addresses passed?
          return out;
        }
     }
     void main() {
       M *op1;
       M *op2;
       M *sol;
       Foo mstorage; // and assume already got valid M objects pointed to by arryM[] elements
       ...
       // Q4: which one correct?
       op1 = mstorage.getM(x); // x being the correct and valid int index pos
       op1 = *mstorage.getM(x); // so address and not object pointed to is passed?
       *op1 = mstorage.getM(x); 
  
       op2 = mstorage.getM(y);
       ... // perform some operations using op1 and op2 M functions
       int size = op1-&gt;getDim() + op2-&gt;getDim();
       sol = new M(size, name); // no default constructor--M objects need a size parameter
       ...// perform/set values of sol based upon op1, op2 values
       // Q5: this is correct to pass the address for sol object created, right?
       if (&lt;sol already exists&gt;) mstorage.replace(indx, *sol);
       else mstorage.add(*sol);
     }
  </code></pre>
  
  <p>So my 5 main questions extracted from the code sample are below, and Q1, Q3, Q4 are most important/confusing to me:</p>
  
  <p>Q1 (important): if an array[i] returns a pointer *n, then how can we dereference "array[i]" to look like "n" so we can get "n=p" where "*p" has an address of an externally created object. Also, where "*p" is passed through a function call with type&amp; parameter.</p>
  
  <p>Q2 : in the given context, is it better to return a pointer or an address in the function? Or would it be easier to change the context to work with one function return type over the other?</p>
  
  <p>Q3 (important): if we are returning an address, what is the correct way to get "array[i]" (based on same assumption as in Q1) to be the address contained in "n" and not object represented by "*n"?</p>
  
  <p>Q4 (important): if a function is returning an address &amp;, then is a locally created pointer *p able to receive an address by "p = obj.funcretaddress()"? I saw some example code with doubles though that made me confused: "double hours = funcreturningaddress()".</p>
  
  <p>Q5 : just double checking that if a function parameter takes "obj &amp;x" and we locally create a "obj *y" that in the function call we would pass "function(*y)" and then within the function scope "obj *n = &amp;x" so that a pointer represented by array[i] could take correct address of the locally created obj.</p>
  
  <p>Many thanks, as I have been compiling and tweaking for several days now, getting confused trying to take examples with int and double data types from the web and apply them to created class objects. </p>
  
  <p>My apologies and please let me know if anything unclear!</p>
  
  <p>edit: Many thanks, and just wanted to clarify the Q4:</p>
  
  <pre><code>    int a = 2;
      int *b;
      b = &amp;a;
      *b = 3;
      int c = 4;
      b = func(c); // *b now points to c, and value is 5?
      *b = func(c); // *b value is updated to 5, and a therefore also now 5??
      ...
      int &amp; func(int &amp;d) {
        d++;
        return &amp;d; // correct, or wrong as &amp;&amp;d is returned?
        return d; // and the &amp; is applied to the var d?
      }
  </code></pre>