Is it possible to have anonymous, ad-hoc arrays in C?

前端 未结 1 743
有刺的猬
有刺的猬 2020-12-18 18:54

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

相关标签:
1条回答
  • 2020-12-18 19:35

    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
    
    0 讨论(0)
提交回复
热议问题