34

我已经声明了 a struct,并尝试将这些结构的数组(以及double双精度数组和整数)传递给函数。编译时,我从 gcc收到“数组类型的元素类型不完整”消息。我在如何将函数传递struct给函数时做错了什么?

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[],但我得到了同样的东西。

4

3 回答 3

38

这是在以下方面引起麻烦的数组:

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() 中,我试图传递给函数的变量是 a 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 (Based on 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
于 2012-04-04T00:35:12.803 回答
9

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

void print_graph(g_node graph_node[], double weight[][5], int nodes);
于 2012-04-04T00:36:58.480 回答
3

[]发布此内容以防有人遇到此问题并想知道通常有效和无效的正式原因[][]。有各种规则在起作用:有效数组声明的规则以及数组如何作为参数传递给函数的规则“衰减”为指向第一个元素的指针。

C17 6.7.6.2/1 数组声明器:

元素类型不应是不完整类型或函数类型。

在 的情况下double weight[][],元素类型是double[],一个不完整的(数组)类型,不允许在任何地方声明,参数与否。因为此数组声明规则适用于函数参数的“数组衰减”规则,该规则可在 C17 6.7.6.3/7 函数声明符中找到:

将参数声明为 ''array of type'' 应调整为 ''qualified pointer to type''</p>

该规则假定我们已经有一个数组声明,这必须按照前面引用的 6.7.6.2 规则来完成。

如果是一维数组double[],那么这是一个不完整的数组类型,但元素类型是double,这是一个完整的类型。根据 C17 6.7.6.2/4 允许这样的数组声明:

如果大小不存在,则数组类型是不完整的类型。

每当这样的数组与初始值设定项列表一起使用时,double foo[] = { 1.0f };C17 6.7.9/22 都会声明它的大小取决于初始值设定项,并在声明结束时变为完整类型:

如果初始化未知大小的数组,则其大小由具有显式初始化程序的最大索引元素确定。数组类型在其初始值设定项列表的末尾完成。

如果它没有被初始化,而只是函数参数列表的一部分,那么前面提到的“数组衰减”规则适用double[]并被替换为double*.

现在,如果我们有一个数组参数,例如double [][3],那么它是一个不完整的数组类型,但元素类型double [3]是一个完整的数组类型,所以它是一个有效的声明。在这种情况下,参数被调整为指向这种元素类型的指针,double (*)[3]. 这就是为什么可以省略多维数组参数声明中最左边的数组维度的原因 - 实际上我们在那里键入的大小无关紧要。

于 2021-03-02T09:38:44.227 回答