Is it possible to create anonymous, ad-hoc arrays in C?
For example, suppose I have a function called processArray(int[] array)
that takes an int array
With C99 and C11, you can write what you wrote, as exemplified by the following code. These are 'compound literals', described in ISO/IEC 9899:2011 §6.5.2.5 Compound literals (and it is the same section in ISO/IEC 9899:1999).
#include <stdio.h>
static void processArray(int n, int arr[])
{
for (int i = 0; i < n; i++)
printf(" %d", arr[i]);
putchar('\n');
}
int main(void)
{
processArray(4, (int[]){0, 1, 2, 3});
return 0;
}
When run, it produces the answer:
0 1 2 3