title: Why do <const char*> and <const char[]> have very different memory or pointer behaviour?
body: <p>I was experimenting with C++ and found out that <code>const char*</code> and <code>const char[]</code> behave very differently with the following code. Really sorry if I did not phrase this question very well as I am not clear of what is happening in the code.</p>
  <pre><code>#include &lt;iostream&gt;                                                                                                        
  #include &lt;vector&gt;                                                                                                          
  
  // This version uses &lt;const char[3]&gt; for &lt;myStr&gt;.
  // It does not work as expected.                                                                                                                      
  struct StrStruct                                                                                                           
  {                                                                                                                  
      const char myStr[3];                                                                                                     
  };                                                                                                                         
         
  // This program extracts all the string elements in &lt;strStructList&gt; and copy them to &lt;strListCopy&gt;                                                                      
  int main()
  {
      StrStruct strStruct1{&quot;ab&quot;};
      StrStruct strStruct2{&quot;de&quot;};
      StrStruct strStruct3{&quot;ga&quot;};
                                                                                                                             
      std::vector&lt;StrStruct&gt; strStructList{strStruct1, strStruct2, strStruct3};
      std::vector&lt;const char*&gt;  strListCopy{};
                                                                                                                             
      for (StrStruct strStructEle : strStructList)                                                                           
      {                                                                                                                      
          strListCopy.push_back(strStructEle.myStr);                                                                         
                                                                                                                             
          std::cout &lt;&lt; &quot;Memory address for the string got pushed back in is &quot;                                                
                    &lt;&lt; &amp;strStructEle.myStr &lt;&lt; std::endl;                                                                     
          std::cout &lt;&lt; &quot;Memory address for the first element of the string got pushed back in is &quot;                           
                    &lt;&lt; (void *) &amp;strStructEle.myStr[0] &lt;&lt; &quot;\n&quot; &lt;&lt;std::endl;                                                          
      }                                                                                                                      
      
      std::cout &lt;&lt; &quot;Show content of &lt;strListCopy&gt;:&quot; &lt;&lt; std::endl;                                                                                                                     
      for (const char*&amp; strEle : strListCopy)                                                                                
      {                                                                                                                      
          std::cout &lt;&lt; strEle &lt;&lt; std::endl;                                                                                  
      }                                                                                                                                                                                            
  }
  </code></pre>
  <p>The following is its output:</p>
  <pre><code>Memory address for the string got pushed back in is [address#99]
  Memory address for the first element of the string got pushed back in is [address#99]
  
  Memory address for the string got pushed back in is [address#99]
  Memory address for the first element of the string got pushed back in is [address#99]
  
  Memory address for the string got pushed back in is [address#99]
  Memory address for the first element of the string got pushed back in is [address#99]
  
  Show content of &lt;strListCopy&gt;:
  ga
  ga
  ga
  </code></pre>
  <p>However, if I just simply change the implementation for <code>StrStruct</code></p>
  <p>from:</p>
  <pre><code>// This version uses &lt;const char[3]&gt; for &lt;myStr&gt;.
  // It does not work as expected. 
  struct StrStruct                                                                                                           
  {                                                                                                                  
      const char myStr[3];                                                                                                     
  };
  </code></pre>
  <p>to</p>
  <pre><code>// This version uses &lt;const char*&gt; for &lt;myStr&gt;.
  // It works as expected.                                                                                                                      
  struct StrStruct                                                                                                           
  {                                                                                                                  
      const char* myStr;                                                                                                     
  };
  </code></pre>
  <p>Program's output becomes this:</p>
  <pre><code>Memory address for the string got pushed back in is [address#10]
  Memory address for the first element of the string got pushed back in is [address#1]
  
  Memory address for the string got pushed back in is [address#10]
  Memory address for the first element of the string got pushed back in is [address#2]
  
  Memory address for the string got pushed back in is [address#10]
  Memory address for the first element of the string got pushed back in is [address#3]
  
  Show content of &lt;strListCopy&gt;:
  ab
  de
  ga
  </code></pre>
  <p>What confuses me is the following:</p>
  <ol>
  <li><p>Why in the first version all the strings have the same value? I tried to use <code>const strStruct&amp;</code> instead of <code>strStruct</code> in the for each loop which solves the problem but I do not understand how.</p>
  </li>
  <li><p>Why do <code>const char*</code> and <code>const char[]</code> behave so differently? I thought  they are largely the same due to the following:</p>
  </li>
  </ol>
  <pre><code>const char myChars[] = &quot;abcde&quot;;                                                                                     
  const char* myCharsCopy = myChars;                                                                                  
                                                                                                                          
  std::cout &lt;&lt; myChars &lt;&lt; &quot; vs &quot;  &lt;&lt; myCharsCopy &lt;&lt; std::endl;  
  </code></pre>
  <p>It prints out <code>abcde vs abcde</code> and you can directly assign value of <code>const char[]</code> to <code>const char*</code> without any error.</p>
  <ol start="3">
  <li>Why does changing <code>const char[]</code> to <code>const char*</code> solves the problem?</li>
  </ol>