title: C: Error at accessing dynamically allocated elements
body: <p>I am trying to write a function that searches a certain element. However, it exists with error when I try to access an element. I commented the rows that generate the error in <code>search</code> function.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #define m 2
  
  typedef struct pag {
      int nr;
      int key[m];
      struct pag * child[m + 1];
  }* page;
  
  page init(page B) {
      int i;
      B = malloc(sizeof(struct pag));
  
      for (i = 0; i &lt; m; i++) {
          B-&gt;key[i] = 0;
          B-&gt;child[i] = malloc(sizeof(struct pag));
      }
      B-&gt;child[i] = malloc(sizeof(struct pag));
      return B;
  }
  
  page search(page B, int k) {
      int i;
      if (B == NULL )
          return B;
      // 1. cautare liniara
      for (i = 0; i &lt; m; i++) {
              // 2. find the desired value
          if (B-&gt;key[i] == k) {
              return B;
              // 3. find the value greater or equal, take the left road to the child
          } else if (B-&gt;key[i] &gt;= k) {
              return search(B-&gt;child[i], k); //exists with error here
          }
      }
  
      return search(B-&gt;child[i], k); //also exists with error here
  }
  
  int main() {
      page B = init(B);
  
      if (search(B, 2) == NULL )
          printf("Negasit");
      else
          printf("Gasit");
  
      return 0;
  }
  </code></pre>