title: Accessing and Manipulating an Array with Pointer in C++
body: <p>I am trying to access an array with pointer in my c++ class.</p>
  
  <p>below is my class. </p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Poly
  {
      friend istream &amp;operator&gt;&gt;(istream &amp;, Poly &amp;);
      friend ostream &amp;operator&lt;&lt;(ostream &amp;, const Poly &amp;);
  
  public:
      Poly();
      Poly(int);
      Poly(int, int);
      Poly(const Poly &amp;);
      ~Poly();
  
      int getCoeff(int) const;
      int getMaxPow() const;
      void setCoeff(int, int);
      void setMaxPow(int);
  
      Poly operator+(const Poly &amp;) const;
      Poly operator+=(const Poly &amp;);
  
      Poly operator-(const Poly &amp;) const;
      Poly operator-=(const Poly &amp;);
  
      Poly operator*(const Poly &amp;) const;
      Poly operator*=(const Poly &amp;);
  
      Poly operator=(const Poly &amp;);
  
      bool operator==(const Poly &amp;) const;
      bool operator!=(const Poly &amp;) const;
  
  private:
      int* coeffPtr;
      int maxPow;
  };
  </code></pre>
  
  <p>below are my constructors</p>
  
  <pre><code>#include "poly.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  Poly::Poly() {
      maxPow = 0;
      int eq[1];
      coeffPtr = &amp;eq[0];
      *coeffPtr = 0;
  }
  
  Poly::Poly(int coeff) {
      maxPow = 0;
      int eq[1];
      coeffPtr = &amp;eq[0];
      *coeffPtr = coeff;
  }
  
  Poly::Poly(int coeff, int maxPower) {
      maxPow = maxPower;
      int eq[maxPower+1];
      coeffPtr = &amp;eq[0];
  
      for(int i = 0; i &lt; maxPower; i++)
      {
          *(coeffPtr+i) = 0;
      }
  
      *(coeffPtr+maxPower) = coeff;
  }
  
  Poly::Poly(const Poly &amp;other) {
      int eq[other.maxPow];
      coeffPtr = &amp;eq[0];
      for(int i  = 0; i &lt; other.maxPow; i++)
      {
          *(coeffPtr+i) = other.getCoeff(i);
      }
  }
  
  int Poly::getCoeff(int pow) const{
      return *(coeffPtr+pow);
  }
  </code></pre>
  
  <p>In my main method, the initial call to getCoeff(number) will return correct element in the array, but it seems that everything gets changed after that initial access.</p>
  
  <pre><code>e.g.,
  Poly A(5,7);
  A.getCoeff(7); //returns 5
  A.getCoeff(7); //returns random memory
  </code></pre>
  
  <p>What am I doing it wrong?</p>
  
  <p>Thanks you!</p>