GCC:数组类型具有不完整的元素类型 [英] GCC: array type has incomplete element type
问题描述
我已经声明了一个结构,我尝试这些结构的数组(以及双打的双阵列和一个整数)传递到一个函数。我得到一个数组类型具有不完整的元素类型从GCC消息时,我编译它。我有什么错我是如何通过结构到功能?
typedef结构graph_node {
INT X;
INTÿ;
INT活跃;
} g_node;无效print_graph(g_node graph_node [],双重量[] [],诠释节点);
我也试过结构g_node graph_node []
,但我得到了同样的事情。
这是造成麻烦的数组:
无效print_graph(g_node graph_node [],双重量[] [],诠释节点);
的第二个和后续的尺寸必须给出:
无效print_graph(g_node graph_node [],双重量[] [32],诠释节点);
或者你也可以只给一个指针,指针:
无效print_graph(g_node graph_node [],双**重量,诠释节点);
不过,虽然他们看起来很相似,这是非常不同的内部。
如果您正在使用C99,可以使用可变合格的阵列。从C99标准引用一个例子(部分§6.7.5.2数组声明):
无效fvla(INT男,INT C [M] [M]); //有效:VLA与原型范围无效fvla(INT男,INT C [M] [M]。)//有效:调整自动指向VLA
{
的typedef诠释VLA [M] [M]; //有效:块范围的typedef VLA
结构标记{
INT(* Y)[N]; //无效:Y不是普通的标识符
诠释Z [N]; //无效:Z不是普通的标识符
};
INT D [M]; //有效:汽车VLA
静态INT E [M]; //无效:静态块范围VLA
EXTERN INT F [M]; //无效:F有挂钩,VLA
INT(* S)[M]。 //有效:自动指向VLA
EXTERN INT(* R)[M]。 //无效:r的联动点VLA
静态INT(* Q)[M] =和B; //有效:q是一个静态块指针VLA
}
在注释
问题
[...]在我的main(),我试图传递到该函数的变量是
双阵列[] []
,所以我怎么会传递到函数?通过阵列[0] [0]
到它给了我不兼容的参数类型,就像&放大器;阵列
和&放大器;阵列[0] [0]
块引用>在你的
的main()
,该变量应该是:双阵列[10] [20];
或隐隐有些类似;也许
双阵列[] [20] = {{1.0,0.0,...},...};
您应该能够通过与code是这样的:
typedef结构graph_node
{
INT X;
INTÿ;
INT活跃;
} g_node;无效print_graph(g_node graph_node [],双重量[] [20],诠释节点);INT主要(无效)
{
g_node政[10];
双阵列[10] [20];
INT N = 10; print_graph(克,数组,N);
返回0;
}这编译(对象code)用干净GCC 4.2(i686的-苹果darwin11-LLVM-GCC-4.2(GCC)4.2.1(基于苹果公司建立5658)(LLVM建2336.9.00 )),并与使用命令行上的Mac OS X 10.7.3 GCC 4.7.0:
的/ usr / bin中/ gcc的-O3 -g -std = C99 -Wall -Wextra -c zzz.c
I have declared a struct, and I try to pass an array of those structs (as well as a double array of doubles, and an integer) into a function. I get an "array type has incomplete element type" message from gcc when I compile it. What have I got wrong in how I pass the struct into the function?
typedef struct graph_node { int X; int Y; int active; } g_node; void print_graph(g_node graph_node[], double weight[][], int nodes);
I have also tried
struct g_node graph_node[]
, but I get the same thing.解决方案It's the array that's causing trouble in:
void print_graph(g_node graph_node[], double weight[][], int nodes);
The second and subsequent dimensions must be given:
void print_graph(g_node graph_node[], double weight[][32], int nodes);
Or you can just give a pointer to pointer:
void print_graph(g_node graph_node[], double **weight, int nodes);
However, although they look similar, those are very different internally.
If you're using C99, you can use variably-qualified arrays. Quoting an example from the C99 standard (section §6.7.5.2 Array Declarators):
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 }
Question in comments
[...] In my main(), the variable I am trying to pass into the function is a
double array[][]
, so how would I pass that into the function? Passingarray[0][0]
into it gives me incompatible argument type, as does&array
and&array[0][0]
.In your
main()
, the variable should be:double array[10][20];
or something faintly similar; maybe
double array[][20] = { { 1.0, 0.0, ... }, ... };
You should be able to pass that with code like this:
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; }
That compiles (to object code) cleanly with GCC 4.2 (i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)) and also with GCC 4.7.0 on Mac OS X 10.7.3 using the command line:
/usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra -c zzz.c
这篇关于GCC:数组类型具有不完整的元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!