title: Is it possible to use const_cast on array to change elements?
body: <p>This is more of an academic question since I know to generally avoid <code>const_cast</code>.</p>
  
  <p>But I was working on the exercise in Chapter 3, # 27 of Thinking in C++, Vol. 1.</p>
  
  <blockquote>
    <p>Create a const array of double and a volatile array of double. Index
    through each array and use const_cast to cast each element to
    non-const and non-volatile, respectively, and assign a value to each
    element.</p>
  </blockquote>
  
  <p>I see how to <code>const_cast</code> single vars:</p>
  
  <pre><code>const int i = 0;
  int* j = const_cast&lt;int*&gt;(&amp;i);
  
  *j = 1;    // compiles and runs
  </code></pre>
  
  <p>But can't figure out how to get it to work with an array. The following compiles, but throws a 'bad access' exception, as if <code>const</code> is still there.</p>
  
  <pre><code>const int sz = 10;
  const double cd[sz] {0,1,2,3,4,5,6,7,8,9};
  volatile double vd[sz] {0,1,2,3,4,5,6,7,8,9};
  
  double* cdp = const_cast&lt;double*&gt;(cd);
  double* vdp = const_cast&lt;double*&gt;(vd);
  
  cdp[0] = 99;    // bad access error
  </code></pre>
  
  <p>Where have I got it wrong?</p>