【发布时间】: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)如果您想确保确实出现不以换行符结尾的消息。