title: Is it possible to access struct members using variables in brackets in C?
body: <p>I´m working on a CS50 task where I should create image filters. I have written the code, it works and passes all tests. However part of the code in my blur filter is not well designed in my view. I have copy-pasted a code chunk three times instead of looping over it. I have tried looping it, but could not figure it out. Basically want to access struct members dynamically.</p>
  <p><strong>The struct:</strong></p>
  <pre><code>typedef struct
  {
      BYTE  rgbtBlue;
      BYTE  rgbtGreen;
      BYTE  rgbtRed;
  } __attribute__((__packed__))
  RGBTRIPLE;
  </code></pre>
  <p><strong>The image created from the struct:</strong></p>
  <pre><code>RGBTRIPLE image[height][width]
  </code></pre>
  <p>As you see below I have three code chunks that are almost the same in the loop. The difference is the name of the struct member &quot;rgbtRed&quot;, &quot;rgbtGreen&quot; &amp; &quot;rgbtBlue&quot;.</p>
  <pre><code>for (int k = 0; k &lt; 3; k++)
  {
      // array to put our values in
      int sqr[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1 };
      int tempSum = 0;
  
      if (k == 0)
      {
          if (i &gt; 0)
          {
              sqr[0] = j &gt; 0 ? image[i - 1][j - 1].rgbtRed : -1;
              sqr[1] = image[i - 1][j].rgbtRed;
              sqr[2] = j &lt; width - 1 ? image[i - 1][j + 1].rgbtRed : -1;
          }
  
          sqr[3] = j &gt; 0 ? image[i][j - 1].rgbtRed : -1;
          sqr[4] = image[i][j].rgbtRed;
          sqr[5] = j &lt; width - 1 ? image[i][j + 1].rgbtRed : -1;
  
          if (i &lt; (height - 1))
          {
              sqr[6] = j &gt; 0 ? image[i + 1][j - 1].rgbtRed : -1;
              sqr[7] = image[i + 1][j].rgbtRed;
              sqr[8] = j &lt; width - 1 ? image[i + 1][j + 1].rgbtRed : -1;
          }
      }
      else if (k == 1)
      {
          if (i &gt; 0)
          {
              sqr[0] = j &gt; 0 ? image[i - 1][j - 1].rgbtGreen : -1;
              sqr[1] = image[i - 1][j].rgbtGreen;
              sqr[2] = j &lt; width - 1 ? image[i - 1][j + 1].rgbtGreen : -1;
          }
  
          sqr[3] = j &gt; 0 ? image[i][j - 1].rgbtGreen : -1;
          sqr[4] = image[i][j].rgbtGreen;
          sqr[5] = j &lt; width - 1 ? image[i][j + 1].rgbtGreen : -1;
  
          if (i &lt; (height - 1))
          {
              sqr[6] = j &gt; 0 ? image[i + 1][j - 1].rgbtGreen : -1;
              sqr[7] = image[i + 1][j].rgbtGreen;
              sqr[8] = j &lt; width - 1 ? image[i + 1][j + 1].rgbtGreen : -1;
          }
      }
      else if (k == 2)
      {
          if (i &gt; 0)
          {
              sqr[0] = j &gt; 0 ? image[i - 1][j - 1].rgbtBlue : -1;
              sqr[1] = image[i - 1][j].rgbtBlue;
              sqr[2] = j &lt; width - 1 ? image[i - 1][j + 1].rgbtBlue : -1;
          }
  
          sqr[3] = j &gt; 0 ? image[i][j - 1].rgbtBlue : -1;
          sqr[4] = image[i][j].rgbtBlue;
          sqr[5] = j &lt; width - 1 ? image[i][j + 1].rgbtBlue : -1;
  
          if (i &lt; (height - 1))
          {
              sqr[6] = j &gt; 0 ? image[i + 1][j - 1].rgbtBlue : -1;
              sqr[7] = image[i + 1][j].rgbtBlue;
              sqr[8] = j &lt; width - 1 ? image[i + 1][j + 1].rgbtBlue : -1;
          }
      }
  
      // sum temp.
      float divider = 0.0;
      for (int l = 0; l &lt; 9; l++)
      {
          if (sqr[l] &gt;= 0 &amp;&amp; sqr[l] &lt;= 255)
          {
              tempSum = tempSum + sqr[l];
              divider++;
          }
      }
  
      // calulate average color for each
      avgRGB[k] = round(tempSum / divider);
  }
  </code></pre>
  <p>I did try to create dynamic names as variables using:</p>
  <pre><code>if (k == 0)
  {
    char *color = &quot;rgbtRed&quot;;
  } else if (k == 1)
    char...
  </code></pre>
  <p>And then using that variable as dynamic name to access the struct member with brackets [] instead of (.) as follows:</p>
  <pre><code>...image[i][j][color];
  </code></pre>
  <p>However that did not work and as I´m new to learning C I would much appreciate some guidance to improve my code.</p>
  <p>All the best!</p>