是否有可能在飞行中传递""数组到C函数?

时间:2016-06-15 15:14:00

标签: c arrays function on-the-fly

在Fortran中,可以将动态构建的数组传递给子程序:

var x = JsonConvert.DeserializeObject<ApiResponse>(SampleJson);
foreach (var kvp in x.Data.SelectMany(d => d))
{
    Console.WriteLine(kvp.Key + ": " + kvp.Value);
}
foreach (var kvp in x.Errors.SelectMany(d => d))
{
    Console.WriteLine(kvp.Key + ": " + kvp.Value);
}

是否有可能在C中做类似的事情?这看起来非常基本,但我无法在此找到任何内容,无论是或否。

1 个答案:

答案 0 :(得分:4)

是。可以使用复合文字(因为C99)。

E.g。

#include <stdio.h>

void fun(int *a)
{
    printf("%d\n", a[2]); //prints 72
}

int main(void)
{
    fun((int[]){1, 99, 72});
}

您还可以从链接中找到更多示例:

  1. The New C: Compound Literals
  2. Compound Literals - gcc