Search code examples
cfunctionreturnmallocvariable-length-array

Return VLA and usage


I have the following function:

int* create_matrix_2(int rows, int cols)
{
    double (*A)[rows][cols] = malloc(sizeof(int[rows][cols]));

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < cols; col++)
        {
            *A[row][col] = row * cols + col;
        }
    }

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < cols; col++)
        {
            printf("%lf, " , *A[row][col]);
        }
        printf("\n");
    }
    return A;
}

My question is: How do I return the VLA from that function, what is the type and how do I write that as a function signature and how do I use it where I receive the returned array?

At the moment I am trying to do it like this:

int (*matrix2)[height][width] = create_matrix_2(height, width);

for (int row = 0; row < height; row++)
{
    for (int col = 0; col < width; col++)
    {
        printf("%d" , (*matrix2)[row][col]);
    }
    printf("\n");
}

and then run: gcc 2d_array_test.c -o 2d_array_test.out -std=c99 -O0

But this results in the following problems:

2d_array_test.c: In function ‘main’:
2d_array_test.c:35:34: warning: initialization from incompatible pointer type [enabled by default]
  int (*matrix2)[height][width] = create_matrix_2(height, width);
                                  ^
2d_array_test.c: In function ‘create_matrix_2’:
2d_array_test.c:105:2: warning: return from incompatible pointer type [enabled by default]
  return A;
  ^

EDIT#1:

I tried to use the code suggested by alk, but it gives me a lot of errors while compiling. Here is a separate programm, which contains your suggested code with a main function: http://pastebin.com/R6hKgvM0 I get the following errors:

2d_array_test_new.c: In function ‘main’:
2d_array_test_new.c:18:2: warning: passing argument 3 of ‘create_matrix’ from incompatible pointer type [enabled by default]
  if (-1 == create_matrix(height, width, &matrix))
  ^
2d_array_test_new.c:10:5: note: expected ‘int **’ but argument is of type ‘int (**)[(sizetype)(height)][(sizetype)(width)]’
 int create_matrix(size_t, size_t, int**);
     ^
2d_array_test_new.c: At top level:
2d_array_test_new.c:37:5: error: conflicting types for ‘create_matrix’
 int create_matrix(size_t rows, size_t cols, int(**a)[rows][cols])
     ^
2d_array_test_new.c:10:5: note: previous declaration of ‘create_matrix’ was here
 int create_matrix(size_t, size_t, int**);
     ^
2d_array_test_new.c: In function ‘create_matrix’:
2d_array_test_new.c:45:11: error: ‘EINVAL’ undeclared (first use in this function)
   errno = EINVAL;
           ^
2d_array_test_new.c:45:11: note: each undeclared identifier is reported only once for each function it appears in
2d_array_test_new.c:40:6: warning: variable ‘errno’ set but not used [-Wunused-but-set-variable]
  int errno;
      ^

The errors mainly seem to be about the return type. How do I write the type of that array correctly?


Solution

    • refering the 1st warning:

      warning: initialization from incompatible pointer type
      

      Here

      int (*matrix2)[height][width] = create_matrix_2(height, width);
      

      int (*matrix2)[height][width] and int * simply aren't the same.

    • refering the 2nd warning:

      warning: return from incompatible pointer type 
      

      This dues to defining

      double (*A)[rows][cols] = malloc(sizeof(int[rows][cols]));
      

      while returning A from int * create_matrix_2().

      int * and double (*A)[rows][cols] also aren't the same.

    I propose you change the function like this:

    #include <errno.h> /* for errno and EINVAL */
    #include <stdlib.h> /* for malloc() */
    
    
    int create_matrix_2(size_t rows, size_t cols, int(**a)[rows][cols])
    {
      int result = 0;
    
      if (NULL == a)
      {
        result = -1;
        errno = EINVAL;
      }
      else
      {
        (*a) = malloc(sizeof **a);
        if (NULL == (*a))
        {
          result = -1;
        }
        else
        {
          for (size_t row = 0; row < rows; row++)
          {
            for (size_t col = 0; col < cols; col++)
            {
              (**a)[row][col] = row * cols + col;
            }
          }
    
          for (size_t row = 0; row < rows; row++)
          {
            for (size_t col = 0; col < cols; col++)
            {
              printf("%d, " , (**a)[row][col]);
            }
    
            printf("\n");
          }
        }
      }
    
      return result;
    }
    

    and call it like this:

    #include <stdlib.h>
    #include <stdio.h>  
    
    
    int create_matrix_2(size_t rows, size_t cols, int(**a)[rows][cols]);
    
    
    int main(void)
    {
      int result = EXIT_SUCCESS;
    
      int (*matrix2)[height][width] = NULL;
      if (-1 == create_matrix_2(height, width, &matrix2))
      {
        perror("create_matrix_2() failed");
        result = EXIT_FAILURE;
      }
      else
      {
        for (size_t row = 0; row < height; row++)
        {
          for (size_t col = 0; col < width; col++)
          {
            printf("%d, " , (*matrix2)[row][col]);
          }
    
          printf("\n");
        }
    
        free(matrix2);
      }
    
      return result;
    }