Why am I getting Segmentation fault (core dumped) when I do not return a pointer of my function?
<p>I have the following code:</p>

<pre><code>void add_tree_nodes(tree_node* root, int nodes[], int size);

int main()
{

  int tree_nodes[] = {3, 1, 0, 2, 8, 6, 5, 9};
  tree_node* root = NULL;
  add_tree_nodes(root, tree_nodes, 8);

  printf("%d\n", root-&gt;data);
  printf("%d\n", root-&gt;left-&gt;data);
  printf("%d\n", root-&gt;right-&gt;data);

  return 0;
}

void add_tree_nodes(tree_node* root, int nodes[], int size)
{
  for (int i = 0; i &lt; size; i++) {
    root = add_tree_node(root, nodes[i]);
  }
}
</code></pre>

<p>And when I run it I am getting <strong>Segmentation fault (core dumped).</strong> However, if I changed my function <code>add_tree_nodes()</code> to return <code>tree_node*</code> it works properly:</p>

<pre><code>tree_node* add_tree_nodes(tree_node* root, int nodes[], int size);

int main()
{

  int tree_nodes[] = {3, 1, 0, 2, 8, 6, 5, 9};
  tree_node* root = NULL;
  root = add_tree_nodes(root, tree_nodes, 8);

  printf("%d\n", root-&gt;data);
  printf("%d\n", root-&gt;left-&gt;data);
  printf("%d\n", root-&gt;right-&gt;data);

  return 0;
}

tree_node* add_tree_nodes(tree_node* root, int nodes[], int size)
{
  for (int i = 0; i &lt; size; i++) {
    root = add_tree_node(root, nodes[i]);
  }
  return root;
}
</code></pre>

<p>My question is why am I getting a Segmentation fault in the first piece of code? I thought that when I passed a pointer into a function the address of that block of code was passed, so whatever I do inside a function affects that thing the pointer is pointing.</p>