Declaring a C function to return an array

Several things to point out.

First of all, you cannot assign an array object as you do here:

char A[WIDTH][HEIGHT];  
A=rand_grid(WIDTH,HEIGHT);

Objects of array type are not modifiable.

Secondly, functions in C cannot return array types. They can return pointers to arrays, though:

char (*foo(int width))[HEIGHT]
{
  /**
   * dynamically allocate memory for a widthxHEIGHT array of char
   */
  char (*newArr)[HEIGHT] = malloc(sizeof *newArr * width);
  /**
   * initialize array contents here
   */
  return newArr;
}

The syntax is a little confusing; it reads as

       foo                                   -- foo
       foo(int width)                        -- is a function
                                             -- taking an int parameter
      *foo(int width)                        -- returning a pointer
     (*foo(int width))[HEIGHT]               -- to a HEIGHT-element array
char (*foo(int width))[HEIGHT]               -- of char

For C89, HEIGHT in the above snippet must be a compile-time constant integral expression (either a macro, a numeric literal, or an arithmetic expression consisting of macros and/or numeric literals). I’m not sure if that’s also true for C99.

Based on the snippet you’ve posted, what you want to do is to take an array you’ve already allocated and initialize its contents. Remember that in most contexts, an expression of an array type will implicitly be converted to a pointer to the base type. IOW, if you pass an N-element array of T to a function, what the function actually receives is a pointer to T:

void foo (T *p) {...}
...
T arr[N];
foo(arr);

For 2-d arrays, it’s a little uglier:

void foo (T (*p)[M]) {...}
...
T arr[N][M];
foo(arr);

This also relies on M being known at compile time, which limits the function’s usefulness. What you’d like is a function that can deal with a 2-d array of arbitrary size. The best way I know of to accomplish this is instead of passing a pointer to the array, pass the address of the first element in the array[1], and pass the number of rows and columns as separate parameters:

void foo(T *base, size_t rows, size_t cols) {...}
...
T arr[N][M];
foo (&arr[0][0], N, M);

So your rand_grid function would look something like this:

void rand_grid(char *base, size_t rows, size_t cols)
{
  size_t i, j;
  for (i = 0; i < rows; i++)
  {
    for (j = 0; j < cols; j++)
    {
      /**
       * Since base is a simple char *, we must index it
       * as though it points to a 1-d array.  This works if
       * base points to the first element of a 2-d array,
       * since multi-dimensional arrays are contiguous.  
       */
      base[i*cols+j] = initial_value();
    }
  }
}

int main(void)
{
  char A[WIDTH][HEIGHT];
  rand_grid(&A[0][0], WIDTH, HEIGHT);
  ...
}

  1. Even though the expressions &A[0][0] and A yield the same value (the base address of A), the types of the two expressions are different. The first expression evaluates to a simple pointer to char (char *), while the second evaluates to a pointer to a 2-d array of char (char (*)[HEIGHT]).

Leave a Comment