title: strcpy() to copy string from struct member to char array is failing. Why?
body: <p>I have a structure  <code>node</code> which is used to create a binary search tree. Inside each node I am storing a integer <code>KEY</code> and a corresponding string <code>value</code>. I am performing some searches within the tree and wish to return arrays containing key value pairs of only specific nodes.</p>
  <p>TO do so I am passing arrays by reference and saving the integer <code>KEY</code> to that array. This works fine, however when I try to the the same with the string I am getting poor results.</p>
  <p>In the below code I am trying to copy the string inside <code>root[count].value;</code> to <code>p_value_arr[*p_unique_count]</code> which is a char array.</p>
  <p><strong>Struct definition</strong>:</p>
  <pre><code>   typedef struct node {
          int KEY;
          char *value;
          int node_count;
          struct node *left, *right;
          int unique_count;
      } node;
  </code></pre>
  <p>Function to traverse graph and copy unique key value pairs. <code>KEY</code> is being copied correctly to an array while <code>value</code> is not.</p>
  <pre><code>void unique_key(node *root, int *p_unique_count, int p_unique_arr[], char *p_value_arr[]) { 
      int count = 0;      
      //unique *temp = (unique *)malloc(n * sizeof(unique));
          
      if (root != NULL)
      {
          unique_key(root-&gt;left, p_unique_count, p_unique_arr, p_value_arr);
          if (root-&gt;node_count == 1) {
  
              root[count].unique_count = *p_unique_count;
              p_unique_arr[*p_unique_count] = root[count].KEY;
              printf(&quot;%s\n&quot;, root[count].value);
              //&quot;warning: assignment makes integer from pointer without a cast&quot;
              strcpy(p_value_arr[*p_unique_count],root[count].value);  
                          
              printf(&quot;%d(%d) -&gt; %s %d\n&quot;, root-&gt;KEY, root-&gt;node_count, root-&gt;value, root-&gt;unique_count);
                          (*p_unique_count)++;
              count++;
          }
          unique_key(root-&gt;right, p_unique_count, p_unique_arr, p_value_arr);
      }
  }
  </code></pre>
  <p>A utility function to <strong>insert a new node</strong> with given key in BST</p>
  <pre><code>node* insert_node(node* node, int key, char *value)
  {
      /* If the tree is empty, return a new node */
      if (node == NULL) 
          return newNode(key,value);
  
      // If key already exists in BST, icnrement count and return 
      if (key == node-&gt;KEY)
      {
          (node-&gt;node_count)++;
      //  return node;
      }
  
      /* Otherwise, recur down the tree */
      if (key &lt; node-&gt;KEY)
          node-&gt;left = insert_node(node-&gt;left, key, value);
      else
          node-&gt;right = insert_node(node-&gt;right, key, value);
  
      /* return the (unchanged) node pointer */
      return node;
  }
  
  node *newNode(int KEY, char *value)
  {
      struct node *temp = (struct node *)malloc(sizeof(struct node));
      temp-&gt;KEY = KEY;
      strcpy(temp-&gt;value, value);
      temp-&gt;left = temp-&gt;right = NULL;
      temp-&gt;node_count = 1;
      return temp;
  }
  </code></pre>
  <p><strong>Main driver code</strong></p>
  <pre><code>int main() {
      int unique_count = 0;
      int in_count = 0;
      int unique_arr[10]; /
      char *value_arr[10];   // an array of pointers  
      
      /* Let us create following BST.  Passing values along with key */
      node *root = NULL;
      
      //this is for storing commands 
      root = insert_node(root, 2, &quot;Hello&quot;);
      root = insert_node(root, 3, &quot;Thanks&quot;);
  
      printf(&quot;\nkeys of the given tree \n&quot;);
      unique_key(root, &amp;unique_count, unique_arr, *value_arr);
      for(int i = 0; i &lt; 10; i++) {                
          printf(&quot;%d %s\n&quot;, unique_arr[i], value_arr[i]);  //Mismatching the argument type &quot;char&quot; and conversion specifier &quot;s&quot; and nothing prints here
      }
     
  }
  </code></pre>
  <p><strong>Output:</strong></p>
  <blockquote>
  <p>Hello</p>
  <p>keys of the given tree</p>
  <p>Segmentation fault</p>
  </blockquote>
  <p>Any suggestions on how I can effectively copy a string inside a struct member to an array of chars?</p>
  <hr />
  <p><strong>EDIT:</strong></p>
  <p><strong>Full code:</strong> <a href="https://pastebin.com/CB4Gp0gY" rel="nofollow noreferrer">https://pastebin.com/CB4Gp0gY</a></p>
  <p>Since <code>char *value_arr[10];</code> is an array of pointers I followed chapter 5.6 of K&amp;R The C programming language to pass the array of pointers to the function. I get no warnings now, but the seg fault persists.</p>
  <p>I also have <code>more warnings</code> set on my NetBeans 8.2.</p>
  <p>Output from debugger:</p>
  <pre><code>/cygdrive/C/Users/****/AppData/Roaming/NetBeans/8.2/bin/nativeexecution/dorun.sh: line 71: 16516 Segmentation fault      (core dumped) sh &quot;${SHFILE}&quot;
  </code></pre>