title: C 2d array Class
body: <p>I have to create a new <code>img_t</code> with initial size rows and cols. If successful(memory allocation succeeds), returns a pointer to a newly allocated <code>img_t</code>.</p>
  <p>I am not able to initialize rows and cols.</p>
  <pre><code>typedef struct { 
      uint8_t** pixels;
      unsigned int rows;
      unsigned int cols;
  } img_t;
  
  /// A type for returning status codes 
  
  typedef enum {
      IMG_OK,
      IMG_BADINPUT,
      IMG_BADARRAY,
      IMG_BADCOL,
      IMG_BADROW,
      IMG_NOTFOUND
  } img_result_t;
  
  //something is wrong in this constructor*******
  img_t* img_create(unsigned int rows, unsigned int cols){
          img_t **arr = malloc(rows * sizeof(img_t*));
          for(int i = 0; i &lt; rows; i++){
             arr[i] = malloc(cols * sizeof(img_t));
             arr[i]-&gt;rows = rows;
        
             return arr[i]; 
          }
          return 0; 
      }
  
  // helper function that prints the content of the img
  void print_img(img_t* im) {
      if (im == NULL) {
          printf(&quot;Invalid img (null).\n&quot;);
          return;
      }
  
      printf(&quot;Printing img of row length %d and col length %d:\n&quot;, im-&gt;rows, im-&gt;cols);
      for (unsigned int i=0; i&lt;im-&gt;rows; i++) {
          for (unsigned int j=0; j&lt;im-&gt;cols; j++) {
              printf(&quot;%d &quot;, im-&gt;pixels[i][j]);
          }
          printf(&quot;\n&quot;);
      }
      printf(&quot;\n&quot;);
  }
  int main() {
      // test variables to hold values returned by the functions
      img_t* test_im = NULL;
      img_result_t test_result = IMG_OK;
  
      // test task 01 &amp; 02
      printf(&quot;Creating test_im by calling 'img_create(10, 10)'\n&quot;);
      test_im = img_create(10, 10);
      if (test_im == NULL) {
          printf(&quot;test_im == NULL\n&quot;);
          return 1; //exit with a non-zero value
      }
      printf(&quot;test_im\n&quot;);
      printf(&quot;Printing test_im\n&quot;);
      print_img(test_im);
  }
  </code></pre>
  <p>Program output:</p>
  <pre><code>test_im
  Printing test_im
  Printing img of row length 10 and col length 0:
  </code></pre>