【问题标题】:C error: array type has incomplete element typeC 错误:数组类型的元素类型不完整
【发布时间】:2012-05-01 07:55:43
【问题描述】:

我正在尝试在 Ubuntu 11.04 中编译一个在 Windows 中运行良好的程序,但它给出了上述错误。我已在导致错误的行中添加了注释。代码如下:

route_input() {
    int num_routes;//Variable to act as the loop counter for the loop getting route details
    int x;

    char route_id[3];
    char r_source[20];
    char r_destination[20];
    int r_buses;



    printf("Please enter the number of routes used: \n");
    scanf("%d", &num_routes);
    char routes_arr[num_routes][10];//An array to hold the details of each route

    printf("\nNumber of routes is %d\n", num_routes);

    struct route r[num_routes];//An array of structures of type route (This line causes the error)

    fflush(stdin);

    for (x = num_routes; x > 0; x--) {
         printf("\nEnter the route number: ");
         scanf("%s", r[x].route_num);
         printf("Route number is %s", r[x].route_num);


         printf("\nEnter the route source: ");
         fflush(stdin);
         scanf("%s", r[x].source);
         printf("Source = %s", r[x].source);


         printf("\nEnter the route destination: ");
         fflush(stdin);
         gets(r[x].destination);
         printf("Destination = %s", r[x].destination);

         printf("\nEnter the number of buses that use this route: ");
         scanf("%d", &r[x].num_of_buses);
         printf("Number of buses = %d", r[x].num_of_buses);


    }

    for (x = num_routes; x > 0; x--) {
        printf("\n\n+++Routes' Details+++\nRoute number = %s, Source = %s, Destination = %s, Number of buses for this route = %d\n", r[x].route_num, r[x].source, r[x].destination, r[x].num_of_buses);
    }

}

【问题讨论】:

  • 不要fflush(stdin),这是未定义的行为。
  • struct route是什么,在哪里定义的?
  • 正如@deebee 指出的那样,如果您的struct route 是具有非指针自引用成员的递归数据结构,您很可能会遇到类型不完整的问题。
  • fflush(stdout) 如果您想确保确实出现不以换行符结尾的消息。

标签: c gcc ubuntu struct


【解决方案1】:

错误信息是因为您的struct route 声明不完整。即在某处你有一行写着

struct route;

没有说明结构中的内容。这是完全合法的,并且允许编译器在知道其中包含什么之前就知道该结构的存在。这允许它为不透明类型和前向声明定义指向 struct route 类型项目的指针。

但是,编译器不能使用不完整的类型作为数组的元素,因为它需要知道结构的大小来计算数组所需的内存量并计算索引的偏移量。

我想说您忘记包含定义您的路由结构的标头。此外,Ubuntu 的库中可能已经有一个名为 struct route 的不透明类型,因此您可能必须重命名您的结构以避免冲突。

【讨论】:

  • 感谢您的帮助。我包含了包含路由结构定义的头文件,它工作正常。
【解决方案2】:

您需要包含定义struct route 的头文件。
我不确定这是哪个标头,它可能在 Linux 和 Windows 之间有所不同。

在 Linux 中,net/route.h 定义了 struct rtentry,这可能是您需要的。

【讨论】:

    【解决方案3】:

    据我所知,C(至少 GCC 不会)不允许您将变量作为数组索引,这就是它产生该错误的原因。请尝试使用常量。

    多维数组不会发生这种情况,因为在多维数组中行不是互补的,但在单维数组的情况下,数组索引必须是一个变量。

    一些编译器确实允许这样的行为,这就是它不会在 Windows 中产生错误的原因。

    【讨论】:

    • 如果这是问题所在,char routes_arr[num_routes][10] 也会失败。所以他可能正在使用 c99 编译器。
    • 正如我所说的 multidim 数组行变量是可选的,据我所知 ubuntu11.04 GCC 确实给出了它不是基于 C99 的错误
    • 我的意思是,他显然在使用支持变量作为维度的编译器(否则 routes_arr 会失败)。所以他定义r的问题不可能是你说的那样。
    • 但是 2D 数组强制不需要一行,这就是为什么它不会在那里失败......而且路线可以是他之前定义的任何结构,它并不真正需要头文件
    • 我自己测试过
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2014-01-31
    • 2018-10-24
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2013-09-27
    相关资源
    最近更新 更多