GCC:数组类型具有不完整的元素类型

我已经声明了一个结构,我尝试将这些结构的数组(以及双数组和一个整数)传递给一个函数。 当我编译它时,我从gcc得到一个“数组类型具有不完整的元素类型”消息。 我如何将结构传递给函数我有什么问题?

typedef struct graph_node { int X; int Y; int active; } g_node; void print_graph(g_node graph_node[], double weight[][], int nodes); 

我也试过了struct g_node graph_node[] ,但我得到了同样的东西。

这是导致问题的arrays:

 void print_graph(g_node graph_node[], double weight[][], int nodes); 

必须给出第二个和后续维度:

 void print_graph(g_node graph_node[], double weight[][32], int nodes); 

或者你可以给指针指针:

 void print_graph(g_node graph_node[], double **weight, int nodes); 

然而,尽管它们看起来相似,但内部却非常不同。

如果您使用的是C99,则可以使用可变限定数组。 引用C99标准中的示例(第6.7.5.2节数组声明符):

 void fvla(int m, int C[m][m]); // valid: VLA with prototype scope void fvla(int m, int C[m][m]) // valid: adjusted to auto pointer to VLA { typedef int VLA[m][m]; // valid: block scope typedef VLA struct tag { int (*y)[n]; // invalid: y not ordinary identifier int z[n]; // invalid: z not ordinary identifier }; int D[m]; // valid: auto VLA static int E[m]; // invalid: static block scope VLA extern int F[m]; // invalid: F has linkage and is VLA int (*s)[m]; // valid: auto pointer to VLA extern int (*r)[m]; // invalid: r has linkage and points to VLA static int (*q)[m] = &B; // valid: q is a static block pointer to VLA } 

评论中的问题

[…]在我的main()中,我试图传递给函数的变量是一个double array[][] ,那么如何将它传递给函数呢? 将array[0][0]传递给它给出了不兼容的参数类型, &array&array[0][0]

在你的main() ,变量应该是:

 double array[10][20]; 

或类似的东西; 也许

 double array[][20] = { { 1.0, 0.0, ... }, ... }; 

您应该可以使用以下代码传递:

 typedef struct graph_node { int X; int Y; int active; } g_node; void print_graph(g_node graph_node[], double weight[][20], int nodes); int main(void) { g_node g[10]; double array[10][20]; int n = 10; print_graph(g, array, n); return 0; } 

用GCC 4.2(i686-apple-darwin11-llvm-gcc-4.2(GCC)4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.9.00))以及GCC编译(到目标代码)使用命令行在Mac OS X 10.7.3上使用4.7.0:

 /usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra -c zzz.c 

编译器需要知道二维数组中第二维的大小。 例如:

 void print_graph(g_node graph_node[], double weight[][5], int nodes);