C错误:数组类型具有不完整的元素类型

dia*_*t66 3 c ubuntu gcc struct

我正在尝试在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);
    }

}
Run Code Online (Sandbox Code Playgroud)

Jer*_*myP 5

由于您的声明不完整而导致错误消息struct route.即某个地方你有一条线说

struct route;
Run Code Online (Sandbox Code Playgroud)

没有规范结构中的内容.这是完全合法的,并允许编译器在知道结构存在之前知道结构存在.这允许它struct route为不透明类型和前向声明定义指向类型项的指针.

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

我会说你忘了包含定义你的路径结构的标题.此外,Ubuntu可能已经struct route在其库中调用了opaque类型,因此您可能必须重命名结构以避免冲突.