title: Using a structure in a recursive function (referenced structure)
body: <p>I'm having problems understanding how to write code that solves the following problem: I have a structure containing a 2D-array. Then I have a recursive function that take a pointer to the structure as an argument and I want the recursive function to be able to manipulate the structure sent, not a local copy. </p>
  
  <p>The struct is initialized in the function initStruct, where memory for the 2D-array is allocated. The recursive function builds up an array and at a specific point calls a function to insert it into the structure's array.</p>
  
  <p>The code: </p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int** spBasis(int);
  void mpBasis(int**, int, int, int, int, int, int, int*, struct mpBasis *, int, int);
  void initMpBasis(struct mpBasis *, int, int);
  void insertMpState(struct mpBasis *, int *);
  
  struct mpBasis {
      int** basis;
      int size;
      int capacity;
  };
  
  int main() {
      int a, b, c, d;
      char maxE[256];
      char noParticles[256];
      char P[256];
      char M[256];
      FILE *fp;
      int **spStates;
      struct mpBasis *mp;
      int mpState[6] = {0, 0, 0, 0, 0, 0};
  
      printf("Input max e for sp states, no of particles, parity (1 for odd and 0 for even) and magnetic projection: ");
  
      gets(maxE);
      gets(noParticles);
      gets(P);
      gets(M);
      spStates = spBasis(atoi(maxE));
      fp = fopen("spStates.txt", "a+");
      fprintf(fp, "E\tj\tl\tm\n");
      for (a = 0; a &lt; 330; a++) {
          fprintf(fp, "State %d: ", a+1);
          for (b = 0; b &lt; 4; b++) {
              fprintf(fp, "%d\t", spStates[a][b]);
          }
          fprintf(fp, "\n");
      }
  
      mp = malloc(sizeof(struct mpBasis));
      initMpBasis(mp, 5449, 6);
  
      for (c = 0; c &lt; 5449; c++) {
          for (d = 0; d &lt; 6; d++) {
              fprintf(fp, "%d: %d\t", c, mp-&gt;basis[c][d]);
          }
          fprintf(fp, "\n");
      }
  
      printf("%p\n", (void*) mp);
  
      printf("hello 3");
      mpBasis(spStates, 0, atoi(maxE), 0, atoi(M), 0, atoi(P), mpState, mp, 0, 0); 
  
      fclose(fp);
      return 0;
  }
  
  int** spBasis(int maxE) {
      int c;
      int i, j, k, l;
      int q = 0;
      int** spStates;
  
      spStates = (int**)malloc(330 * sizeof(int *));
      for (c = 0; c &lt; 330; c++) {
          spStates[c] = malloc(4 * sizeof(int));
      }
  
      for (i = 0; i &lt;= maxE; i++) {
          for (j = i % 2; j &lt;= i; j += 2) {
              for (k = -(2 * j  + 1); k &lt;= (2 * j + 1); k += 2) {
                  spStates[q][0] = i;
                  spStates[q][1] = j;
                  spStates[q][2] = 2 * j + 1;
                  spStates[q][3] = k;
                  q += 1;
              }
              for (l = -(2 * j - 1); l &lt;= (2 * j - 1); l += 2) {
                  spStates[q][0] = i;
                  spStates[q][1] = j;
                  spStates[q][2] = 2 * j - 1;
                  spStates[q][3] = l;
                  q += 1;
              }
          }
      }
      return spStates;
  }
  
  void mpBasis(int** spStates, int e, int maxE, int m, int M, int l, 
      int P, int * mpState, struct mpBasis *mpB, int position, int lastSpState) {
      int i;
  
      for (i = lastSpState; i &lt; 330; i++) {
          if (e &gt; maxE) {
              break;
          } else if (position == 5) {
              if (m == M &amp;&amp; l % 2 == P) {
                   insertMpState(mpB, mpState);
                   break;
               }
          } else {
               // add spState to mpState and make the recursive call for the next position
              mpState[position] = i;
              mpBasis(spStates, e + spStates[i][0], maxE, m + spStates[i][3], M, 
                  l + spStates[i][1], P, mpState, mpB, position+1, i);
          }
      }
  }
  
  void initMpBasis(struct mpBasis *a, int initialSize, int sizeY) {
      int c;
      a-&gt;basis = (int **)malloc(initialSize * sizeof(int*));
      for (c = 0; c &lt; initialSize; c++) {
          a-&gt;basis[c] = (int *) malloc(sizeY * sizeof(int));
      }
      a-&gt;size = 0;
      a-&gt;capacity = initialSize;
  }
  
  void insertMpState(struct mpBasis *a, int* mpState) {
      /*if (a-&gt;size == a-&gt;capacity) {
          a-&gt;size *= 2;
          a-&gt;basis = (int **)realloc(a-&gt;basis, a-&gt;size * sizeof(int));
      }*/
      a-&gt;basis[a-&gt;size++] = mpState;
  }
  </code></pre>
  
  <p>Added all the code.</p>
  
  <p>The problem is that after the recursive function has been called, the "basis" array in structure mpBasis still only contains random values, i.e. the mpBasis function hasn't done anything with it. Am I passing the mp argument by value here?</p>
  
  <p>Thanks for your help!</p>