C error: array type has incomplete element type
Solution 1
The error message is caused because you have an incomplete declaration of struct route
. i.e. somewhere you have a line that says
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 clash.
Solution 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.
In Linux, net/route.h
defines struct rtentry
, which may be what you need.
Trust Birungi
An entrepreneur and technologist focused on the impact that can be realized from the synergies of merging technological innovation with business process innovation.
Updated on June 04, 2022Comments
-
Trust Birungi almost 2 years
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:
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); } }
-
ouah about 12 yearsDo not
fflush(stdin)
, it is undefined behavior. -
deebee about 12 yearspossible duplicate of stackoverflow.com/questions/2274550/…
-
Mat about 12 yearsWhat is
struct route
, where is it defined? -
dirkgently about 12 yearsAs @deebee pointed out, if your
struct route
is a recursive data structure with non-pointer self-referential members, you are likely to hit the incomplete type issue. -
Jens about 12 years
fflush(stdout)
instead if you want to make sure the messages not ending in newlines do appear. -
Gandaro about 12 yearsFull error messages are so underestimated…
-
-
ugoren about 12 yearsIf that was the problem,
char routes_arr[num_routes][10]
would fail also. So he's probably using a c99 compiler. -
Kartik Anand about 12 yearsas i said multidim arrays row variable is optional and as far as i know ubuntu11.04 GCC does give that error it isnt based on C99
-
ugoren about 12 yearsMy point is that he's obviously using a compiler that supports a variable as dimension (or the
routes_arr
would fail). So his problem in definingr
can't be what you say. -
Kartik Anand about 12 yearsBut 2D arrays dont need a row compulsarily that's why it isnt failing there...moreover route can be just any structure he has defined earlier it doesnt really need a header file
-
Kartik Anand about 12 yearsive tested it check it urself
-
Jens about 12 yearsKartik Anand, please educate yourself about variable length arrays. What your particular compiler accepts or doesn't accept is of no importance. The important part is, what the C language definition says.
-
Kartik Anand about 12 years@Jens before commenting why dont u try what i am saying and yes i have educated myself well in C(atleast)...C allows u to define 2D arrays with optional row..but it wont happen with 1D arrays there u have to specify index(constant)..seriously can u please give it a try before blaming other's education...and yes what ur compiler accepts or not does make a differene(Because its a compiler error)...and different versions do exist some accepting variable length arrays and some dont(GCC being one of them)
-
Jens about 12 years@Kartik Anand: "As far as I know C (at least GCC won't) doesn't allow you to have variables as array indexes." is just plain wrong since 1999. Why did you write this sentence? And seriously, could you try proper spelling? Thanks.
-
Kartik Anand about 12 yearsI know C99 added that support but GCC also follows ansi C..Try it on ubuntu and then question me...
-
Kartik Anand about 12 yearsmoreover C99 support is an extension in GCC which can be checked if enabled or not..readthis(if these extentions are off or not used properly they will result in this error)gcc.gnu.org/onlinedocs/gcc/Standards.html
-
Jens about 12 years@Kartik, now please try compiling
main(int argc, char**argv){int x[argc][argc];return 0;}
. Gcc on Ubuntu compiles by default C99 (plus some other extensions). -
Kartik Anand about 12 yearswhat i am trying to say is if these extensions are turned off they will result in this error..try your code with -std..and then tell me the error you are facing...
-
JeremyP about 12 yearsIn C99 it is legal to declare an array with a variable size. This is default behaviour for modern versions of GCC (well C99 + gnu extensions). Anyway, the error message says this is not the problem.
-
ugoren about 12 yearsAlso, read the question headline: "array type has incomplete element type". The problem is with the element type, that's incomplete, not the dimensions.
-
Trust Birungi almost 12 yearsThanks for your help. I included the header file containing the route struct definition and it worked correctly.