title: Accessing and setting array values within structs
body: <p>I have a struct like this:</p>
  
  <pre><code>typedef struct {
      int sizes[3];
      float **vals[3]; // an array of size 3 of float ** pointers
  } mystruct;
  </code></pre>
  
  <p>What I'm trying to do is set these array values in a function that takes a pointer to a <code>mystruct</code>. </p>
  
  <pre><code>void populateStruct(mystruct *s) {
      int i, j, n;
  
      for (n = 0; n &lt; 3; n++) {
          scanf("%d", &amp;s-&gt;sizes[n]);
  
          // Malloc the float pointers (**vals) based on the scanned sizes
          s-&gt;vals[n] = (float **)malloc(s-&gt;sizes[n] * sizeof(float *));
          for (i = 0; i &lt; s-&gt;sizes[n]; i++)
              s-&gt;vals[n][i] = (float *)malloc(s-&gt;sizes[n] * sizeof(float));
  
          // Populate the float "arrays"
          for (i = 0; i &lt; s-&gt;sizes[n]; i++) {
              for (j = 0; j &lt; s-&gt;sizes[n]; j++) {
                  scanf("%f", &amp;s-&gt;vals[n][i][j]);
              }
          }
      }
  }
  </code></pre>
  
  <p>Here is how I'm using the function in <code>main</code>:</p>
  
  <pre><code>int main() {
      mystruct *s1;
      populateStructure(s1);
      return 0;
  }
  </code></pre>
  
  <p>This code compiles fine, but I get a seg fault when I run it. C is not a strong point of mine, so I'm not too sure what I'm doing wrong.</p>