Image

Imageutil wrote in Imagecprogramming

Return an array value?

Is it possible to return an array value in C? I thought it used to be, but I haven't found a way to make it work.

By array value, I mean something legal to use to initialize the value of a statically allocated array. (I'm aware of the possibility of returning a pointer to a dynamically allocated array or to a static element of the function. I am asking about this mostly for curiosity's sake.)

A program that fails to compile and how it fails:
 returnarray.c:
typedef double DoublePair[2];

DoublePair foo(int n)
{
  DoublePair result;
  result[0] = 3 * n;
  result[1] = 4 * n;
  return result;
}

int main()
{
  DoublePair a = foo(5);
  return 0;
}

$ gcc returnarray.c
returnarray.c:4: error: `foo' declared as function returning an array
returnarray.c: In function `foo':
returnarray.c:8: warning: return makes integer from pointer without a cast
returnarray.c:8: warning: function returns address of local variable
returnarray.c: In function `main':
returnarray.c:13: error: invalid initializer