title: Copy an array of string in C
body: <p>I'm making a program which sorts some input words according to some rules.</p>
  
  <p>To allign them, I'd like to copy "words" to "tmp" by using memcpy to use and change "tmp".</p>
  
  <p>I tried to declare tmp to double pointer or array, but the only I met was segmentation fault.</p>
  
  <p>How could I copy all of "words"?</p>
  
  <pre><code>#include &lt;malloc.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX_WORD_LEN 30
  
  int getInput(char*** words) {
    int count;
    int i;
    char buffer[MAX_WORD_LEN+1];
  
    printf("Enter the number of words: ");
    scanf("%d", &amp;count);
    *words = malloc(count * sizeof(char*));
    printf("Enter the words: ");
    for (i = 0; i &lt; count; i++) {
      (*words)[i] = malloc((MAX_WORD_LEN + 1) * sizeof(char));
      scanf("%s", buffer);
      strcpy((*words)[i], buffer);
    }
    return count;
  }
  
  void solve() {
    int count;
    int i;
    char ** words;
    count = getInput(&amp;words);
  
    char ** tmp = malloc(count* sizeof(char*));
    memcpy(tmp, words, sizeof(char *));
  }
  
  void main() {
    solve();
    return;
  }
  </code></pre>