title: c++ char array to char pointer in struct
body: <p>I have a struct containing a char pointer and I have a char array containing data. I would like to have the data within the char array copied to the char pointer.</p>
  <p>I have tried strcpy and strncpy but I get a seg fault on both, when trying to copy the data over. I cant figure out what I am doing wrong.</p>
  <p>struct</p>
  <pre><code>struct Message
  {
      Header hdr;
      char *dataArr;
      Footer ftr;
  };
  </code></pre>
  <p>main</p>
  <pre><code>int main(){
      // create message struct
      Message send1;
  
      // create stream
      std::ostringstream outStream;
  
      //
      // STREAM GETS DATA HERE
      //
  
      std::string temp = outStream.str();
      char arr[temp.size()];
      strcpy(arr, temp.c_str());
      int sz = sizeof(arr)/sizeof(char);
      
      // print arr size and contents
      std::cout &lt;&lt; &quot;arr size: &quot; &lt;&lt; sz &lt;&lt; &quot;\n&quot;;
      for(int i=0; i&lt;sz; i++){
          std::cout &lt;&lt; arr[i];
      }
  
      // copy char array into structs char pointer
      //strncpy(send1.dataArr, arr, sz);
      strcpy(send1.dataArr, arr);   &lt;-- Segmentation fault here
  
      return 0;
  }
  </code></pre>