title: how to return a pointer to a two dimensional array with variable size in c
body: <p>I have the following method to read the colors from a PPM file:</p>
  
  <pre><code>??? getColors(FILE *fd, int width, int height){
  
      COLOR (*colors)[width] = malloc( sizeof(COLOR[height][width]) );
  
      int i,j;
      for(i = 0; i &lt; height; i++) {
          for(j = 0; j &lt; width; j++) {        
              fscanf(fd,"%d %d %d", &amp;colors[i][j].red, &amp;colors[i][j].green, &amp;colors[i][j].blue);  
          }
      }
  
      return colors;
  }
  </code></pre>
  
  <p>where fd is the file, with the pointer pointing at the first color, and width and height are the width and height of the PPM file. What is the type I return?</p>
  
  <p>EDIT:</p>
  
  <p>Just so I understand, I'm returning a pointer 1d array that is a size of a 2d array correct?</p>
  
  <p>so </p>
  
  <pre><code>COLOR (*getColors(FILE *fd, int width, int height))[]{
  
      COLOR (*colors)[width] = malloc( sizeof(COLOR[height][width]) );
  
      int i,j;
      for(i = 0; i &lt; height; i++) {
          for(j = 0; j &lt; width; j++) {        
              fscanf(fd,"%d %d %d", &amp;colors[i][j].red, &amp;colors[i][j].green, &amp;colors[i][j].blue);  
          }
      }
  
      return colors;
  }
  </code></pre>
  
  <p>is what I am returning and Im using it like this:</p>
  
  <pre><code>COLOR *colorValues[];
  
  colorValues = getColors(fd, width, height);
  </code></pre>
  
  <p>right?</p>