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

时间:2021-07-12 22:54:56

I'm trying to compile a program in Ubuntu 11.04 that works well in Windows but it gives the above error. I have added a comment to the line that is causing the error. Here's the code:

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

}

3 个解决方案

#1


5  

The error message is caused because you have an incomplete declaration of struct route. i.e. somewhere you have a line that says

导致错误消息的原因是您有一个不完整的struct路由声明。比如,你有一行写着

struct route;

with no specification of what is in the struct. This is perfectly legal and allows the compiler to know the struct exists before it knows what is in it. That allows it to define pointers to items of type struct route for opaque types and for forward declarations.

没有说明结构中有什么。这是完全合法的,并且允许编译器在知道结构体中有什么之前就知道它的存在。这允许它定义指向类型结构路由的指针,用于不透明类型和转发声明。

However, the compiler cannot use an incomplete type as the elements for an array because it needs to know the size of the struct to calculate the amount of memory needed for the array and to calculate offsets from indexes.

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

I'd say you have forgotten to include the header that defines your route struct. Also, it's possible that Ubuntu has an opaque type called struct route in its library already, so you may have to rename your struct to avoid a *.

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

#2


2  

You need to include the header file that defines struct route.
I'm not sure which header this is, and it may differ between Linux and Windows.

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

In Linux, net/route.h defines struct rtentry, which may be what you need.

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

#3


0  

As far as I know C (at least GCC won't) doesn't allow you to have variables as array indexes, that's why it's producing that error. Try a constant instead.

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

It doesn't happen with multi dimensional arrays because row is not complusary in multi dim arrays but in case of single dim arrays array index must be a variable.

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

Some compilers do allow such behaviour that's why it isn't producing an error in windows.

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

#1


5  

The error message is caused because you have an incomplete declaration of struct route. i.e. somewhere you have a line that says

导致错误消息的原因是您有一个不完整的struct路由声明。比如,你有一行写着

struct route;

with no specification of what is in the struct. This is perfectly legal and allows the compiler to know the struct exists before it knows what is in it. That allows it to define pointers to items of type struct route for opaque types and for forward declarations.

没有说明结构中有什么。这是完全合法的,并且允许编译器在知道结构体中有什么之前就知道它的存在。这允许它定义指向类型结构路由的指针,用于不透明类型和转发声明。

However, the compiler cannot use an incomplete type as the elements for an array because it needs to know the size of the struct to calculate the amount of memory needed for the array and to calculate offsets from indexes.

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

I'd say you have forgotten to include the header that defines your route struct. Also, it's possible that Ubuntu has an opaque type called struct route in its library already, so you may have to rename your struct to avoid a *.

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

#2


2  

You need to include the header file that defines struct route.
I'm not sure which header this is, and it may differ between Linux and Windows.

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

In Linux, net/route.h defines struct rtentry, which may be what you need.

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

#3


0  

As far as I know C (at least GCC won't) doesn't allow you to have variables as array indexes, that's why it's producing that error. Try a constant instead.

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

It doesn't happen with multi dimensional arrays because row is not complusary in multi dim arrays but in case of single dim arrays array index must be a variable.

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

Some compilers do allow such behaviour that's why it isn't producing an error in windows.

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