GCC:数组类型具有不完整的元素类型 [英] GCC: Array type has incomplete element type
问题描述
我已经声明了一个 struct
,并且我尝试将这些结构的数组(以及一个双精度数组和一个整数的 double
数组)传递给一个函数.我在编译时从 gcc 收到 数组类型具有不完整的元素类型" 消息.将 struct
传递给函数的方式有什么问题?
typedef struct graph_node {整数 X;输入 Y;活动;} g_node;void print_graph(g_node graph_node[], double weight[][], int nodes);
我也尝试过 struct g_node graph_node[]
,但我得到了同样的结果.
导致问题的数组:
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]);//有效:具有原型作用域的 VLAvoid fvla(int m, int C[m][m])//有效:调整为自动指向 VLA{typedef int VLA[m][m];//有效:块作用域 typedef VLA结构标签{int (*y)[n];//无效:y 不是普通标识符int z[n];//无效:z 不是普通标识符};int D[m];//有效:自动 VLA静态整数 E[m];//无效:静态块作用域 VLAextern int F[m];//无效:F 有链接并且是 VLAint (*s)[m];//有效:自动指向 VLAextern int (*r)[m];//无效:r 有链接并指向 VLAstatic int (*q)[m] = &B;//有效:q 是指向 VLA 的静态块指针}
<小时>
评论中的问题
<块引用>[...] 在我的 main() 中,我试图传递给函数的变量是一个 double array[][]
,那么我该如何将它传递给函数呢?将 array[0][0]
传入它给了我不兼容的参数类型,&array
和 &array[0][0]代码>.
在你的 main()
中,变量应该是:
双数组[10][20];
或类似的东西;也许
double array[][20] = { { 1.0, 0.0, ... }, ... };
你应该可以用这样的代码传递它:
typedef struct graph_node{整数 X;输入 Y;活动;} g_node;void print_graph(g_node graph_node[], double weight[][20], int nodes);int main(void){g_node g[10];双数组[10][20];整数 n = 10;打印图(g,数组,n);返回0;}
使用 GCC 4.2(i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1(基于 Apple Inc. build 5658)(LLVM build 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 gotten wrong in how I pass the struct
to 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屋!