title: C / C++ How to copy a multidimensional char array without nested loops?
body: <p>I'm looking for a smart way to copy a multidimensional char array to a new destination. I want to duplicate the char array because I want to edit the content without changing the source array.</p>
  
  <p>I could build nested loops to copy every char by hand but I hope there is a better way.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I don't have the size of the 2. level dimension. Given is only the length (rows).</p>
  
  <p>The code looks like this:</p>
  
  <pre><code>char **tmp;
  char **realDest;
  
  int length = someFunctionThatFillsTmp(&amp;tmp);
  
  //now I want to copy tmp to realDest
  </code></pre>
  
  <p>I'm looking for a method that copies all the memory of tmp into free memory and point realDest to it.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>someFunctionThatFillsTmp() is the function credis_lrange() from the Redis C lib <a href="http://code.google.com/p/credis/source/browse/trunk/credis.c" rel="noreferrer">credis.c</a>.</p>
  
  <p>Inside the lib tmp is created with:</p>
  
  <pre><code>rhnd-&gt;reply.multibulk.bulks = malloc(sizeof(char *)*CR_MULTIBULK_SIZE)
  </code></pre>
  
  <p><strong>Update 3:</strong></p>
  
  <p>I've tried to use memcpy with this lines:</p>
  
  <pre><code>int cb = sizeof(char) * size * 8; //string inside 2. level has 8 chars
  memcpy(realDest,tmp,cb);
  cout &lt;&lt; realDest[0] &lt;&lt; endl;
  
  prints: mystring
  </code></pre>
  
  <p>But I'm getting a: <em>Program received signal: EXC_BAD_ACCESS</em></p>