Like Tree1Likes

array type has incomplete element type

This is a discussion on array type has incomplete element type within the C Programming forums, part of the General Programming Boards category; Im down to a deadline and need to solve the problems with this code. When compiling its giving me the ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    42

    array type has incomplete element type

    Im down to a deadline and need to solve the problems with this code. When compiling its giving me the errors:

    [12:22:05][peg002@lnxAcadShell01][/home/peg002/LAB4]+ make
    cc -c -o lib/SelectionSort.o lib/SelectionSort.c
    lib/SelectionSort.c:6: error: array type has incomplete element type
    lib/SelectionSort.c:6: warning: ‚struct employee‚ declared inside parameter list
    lib/SelectionSort.c:6: warning: its scope is only this definition or declaration, which is probably not what you want
    lib/SelectionSort.c: In function ‚SelectionSort‚:
    lib/SelectionSort.c:9: error: array type has incomplete element type
    make: *** [lib/SelectionSort.o] Error 1


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MaxSize 30
    
    void SelectionSort(struct employee eTable[MaxSize], int numEntries, FILE *outFile){
    
    
                    struct employee temp[1];
                    int maxPt;
    
                    int j, k;
                    for(j = (numEntries -1); j > 0; j--){
                            maxPt = 0;
                            for(k = 1; k <= j; k++)
                                    if(strcmp(eTable[k].dept, eTable[maxPt].dept) > 0)
                                            maxPt = k;
    
                            temp = eTable[maxPt];
                            eTable[maxPt] = eTable[j];
                            eTable[j] = temp;
                    }
    
    
                    fprintf(outFile, "\n\n********************SORTED DATA FROM ''D'' OPTION SORT**************************\n\n\n");
    
                    for(k=0; k < numEntries; k++){
                            fprintf(outFile, "%-10s\t%-12s\t%-20s\t%-4.2f\t%-10s\n", eTable[k].surname, eTable[k].given, eTable[k].dept, eTable[k].payRate, eTable[k].eyeColor);
                    }
    
            }
    I'm clueless as to how to fix this. I don't know if its necessary to be able to help, but here is my main program..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <dlfcn.h>
    #define MaxSize 30
    
    
    
     //Define struct//
    struct employee {
            char surname[10];
            char given[12];
            char dept[20];
            float payRate;
            char eyeColor[8];
    };
    
    
    void (*SelectionSort_d)();
    void (*SelectionSort2_d)();
    void (*SelectionSort3_d)();
    void (*TemplateSelectionSort_d)();
    
    
    int main(){
    
            FILE *inFile;
            FILE *outFile;
    
            char resp = 'Y';
            char fileInName[20];
            char fileOutName[20];
    
            int k;
            int numRecords = 0;
    
            //Prompting user for Input file//
            while(resp == 'Y' || resp == 'y'){
                    printf("Please enter the file you'd like to read from: ");
                    scanf("%s", fileInName);
                    if((fopen(fileInName, "r")) == NULL){
                            printf("Could not open requested file. Would you like to enter another file? [Y/N]: ");
                            scanf("%*c %c", &resp);
                    }
                    else {
                            inFile = fopen(fileInName, "r");
                            break;
                    }
                    if(resp == 'n' || resp == 'N')
                            exit(1);
    
            }
    
    
            //Prompting user for Output File//
    
            printf("Please enter the file you'd like to write to: ");
            scanf("%s", fileOutName);
            if(fopen(fileOutName, "r") != NULL){
                    printf("The file you are attempting to write to already exists! Overwrite?? [Y/N]: ");
                    scanf("%*c %c", &resp);
                    if(resp == 'Y' || resp == 'y')
                            outFile = fopen(fileOutName, "w");
                    else {
                            printf("Aborting program to avoid overwriting existing file!!");
                            exit(1);
                         }
             }
    
    
    
            if(fopen(fileOutName, "r") == NULL)
                    outFile = fopen(fileOutName, "w");
    
            struct employee table[MaxSize];
    
            for(k=0;(fscanf(inFile, "%s%s%s%f%s", table[k].surname, table[k].given, table[k].dept, &table[k].payRate, table[k].eyeColor) != EOF); k++){
                    numRecords++;
            }
    
            for(k=0; k<numRecords; k++){
                    fprintf(outFile, "%-10s\t%-12s\t%-20s\t%-4.2f\t%-10s\n", table[k].surname, table[k].given, table[k].dept, table[k].payRate, table[k].eyeColor);
            }
    
    
            (*SelectionSort)(table, numRecords, outFile);
            (*SelectionSort2)(table, numRecords, outFile);
            (*SelectionSort3)(table, numRecords, outFile);
            (*TemplateSelectionSort)(table, numRecords, outFile, "surname");
            (*TemplateSelectionSort)(table, numRecords, outFile, "dept");
    
    }
    Any help would be greatly appreciated!!!!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    That error is because your file with the sort code doesn't know what a struct employee looks like. Move the structure definition into a .h file, and include that .h file in SelectionSort.c and main.c. Make sure to use include guards.
    A few more things:
    1. You also need a prototype for SelectionSort() in a header file to include in main.c
    2. In your sort function, don't make temp an array of size one (that's pretty silly). Just make it struct employee temp. It's causing problems with the assignments on line 20 and 22.
    3. I'm not sure what those function pointers are doing in your main file: void (*SelectionSort_d)(); defines a variable SelectionSort_d that is a pointer to a function, taking any number of arguments and returning nothing. A prototype should look just like the first line of a function, but with a ; at the end: void SelectionSort(struct employee eTable[MaxSize], int numEntries, FILE *outFile);
    4. The correct form is int main(void) and you need to return an int at the end, usually 0.

    That's all I got for now. Make sure the warnings are turned all the way up on your compiler and heed them.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by philgrek;1024977
    [12:22:05
    [peg002@lnxAcadShell01][/home/peg002/LAB4]+ make
    cc -c -o lib/SelectionSort.o lib/SelectionSort.c
    It would appear there is a makefile for this, you might want to post that.

    The crux of the issue is that struct employee is declared and defined in some file that is not SelectionSort.c, and that file is not included in SelectionSort.c, so the first time the compiler has heard of struct employee is here:

    Code:
    void SelectionSort(struct employee eTable[MaxSize], int numEntries, FILE *outFile)
    Which would be a very unorthodox place to declare a new datatype.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    #3. the function pointers are the method I've been told by my prof to use when using dynamic link libraries. they've worked on all the other code i've created.
    #2. when I leave it as
    Code:
    struct employee temp
    i end up getting "array has incomplete element type" for that as well.
    #1 i will take your advice and create a header file and let you know how that works

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    [12:50:39][peg002@lnxAcadShell01][/home/peg002/LAB4]+ cat Makefile
    #Make file to create LAB4 library and executable

    app/LAB4exe: lib/LAB4.c lib/libLAB4lib.so
    gcc -o app/LAB4exe -L. lib/LAb4.c lib/libLAB4lib.so -ldl

    lib/libLAB4lib.so: lib/SelectionSort.o lib/SelectionSort2.o lib/SelectionSort3.o lib/TemplateSort.o
    gcc -shared -o lib/libLAB4lib.so lib/SelectionSort.o lib/SelectionSort2. o lib/SelectionSort3.o lib/TemplateSort.o

    SelectionSort.o: SelectionSort.c
    gcc -fPIC -c lib/SelectionSort.c

    SelectionSort2.o: SelectionSort2.c
    gcc -fPIC -c lib/SelectionSort2.c

    SelectionSort3.o: SelectionSort3.c
    gcc -fPIC -c lib/SelectionSort3.c

    TemplateSort.o: TemplateSort.c
    gcc -fPIC -c lib/TemplateSort.c

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    Quote Originally Posted by philgrek View Post
    #3. the function pointers are the method I've been told by my prof to use when using dynamic link libraries. they've worked on all the other code i've created.
    Do what your prof wants you to do. Seems like a wonky way to do it IMO however.

    i end up getting "array has incomplete element type" for that as well.
    #1 i will take your advice and create a header file and let you know how that works
    That's not because you took the [1] off the definition of temp. It's because SelectionSort.c doesn't know what a struct employee is. Fix that and your error will disappear.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    Ok so I've created a header file to include in main (which I'm almost certain I'm doing wrong...I'm new at header files):
    [code][13:08:14][peg002@lnxAcadShell01][/home/peg002/LAB4]+ cat lib/lib.h
    #ifndef lib.h
    #define lib.h
    #define MaxSize 30

    extern void SelectionSort(struct employee eTable[MaxSize], int, FILE);
    extern void SelectionSort2(struct employee eTable[MaxSize], int, FILE);
    extern void SelectionSort3(struct employee eTable[MaxSize], int, FILE);
    extern void TemplateSelectionSort(struct employee eTable[MaxSize], int, FILE, MyType);

    #endif
    [code]

    Now when compiled with all warnings on for the main program I get:
    [13:08:00][peg002@lnxAcadShell01][/home/peg002/LAB4]+ gcc -Wall -o lib/LAB4.o lib/LAB4.c
    In file included from lib/LAB4.c:4:
    lib/lib.h:1:12: warning: extra tokens at end of #ifndef directive
    lib/lib.h:2:12: warning: missing whitespace after the macro name
    In file included from lib/LAB4.c:4:
    lib/lib.h:5: error: array type has incomplete element type
    lib/lib.h:5: warning: ‚struct employee‚ declared inside parameter list
    lib/lib.h:5: warning: its scope is only this definition or declaration, which is probably not what you want
    lib/lib.h:6: error: array type has incomplete element type
    lib/lib.h:6: warning: ‚struct employee‚ declared inside parameter list
    lib/lib.h:7: error: array type has incomplete element type
    lib/lib.h:7: warning: ‚struct employee‚ declared inside parameter list
    lib/lib.h:8: error: array type has incomplete element type
    lib/lib.h:8: error: expected declaration specifiers or ‚...‚ before ‚MyType‚
    lib/lib.h:8: warning: ‚struct employee‚ declared inside parameter list
    lib/LAB4.c: In function ‚main‚:
    lib/LAB4.c:86: error: type of formal parameter 1 is incomplete
    lib/LAB4.c:86: error: incompatible type for argument 3 of ‚SelectionSort‚
    lib/LAB4.c:87: error: type of formal parameter 1 is incomplete
    lib/LAB4.c:87: error: incompatible type for argument 3 of ‚SelectionSort2‚
    lib/LAB4.c:88: error: type of formal parameter 1 is incomplete
    lib/LAB4.c:88: error: incompatible type for argument 3 of ‚SelectionSort3‚
    lib/LAB4.c:89: error: type of formal parameter 1 is incomplete
    lib/LAB4.c:89: error: incompatible type for argument 3 of ‚TemplateSelectionSort‚
    lib/LAB4.c:89: error: too many arguments to function ‚TemplateSelectionSort‚
    lib/LAB4.c:90: error: type of formal parameter 1 is incomplete
    lib/LAB4.c:90: error: incompatible type for argument 3 of ‚TemplateSelectionSort‚
    lib/LAB4.c:90: error: too many arguments to function ‚TemplateSelectionSort‚

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void SelectionSort(struct employee eTable[MaxSize], int numEntries, FILE *outFile){
    Lose the highlighted part and try it again... just use eTable[]

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    I'm not sure if it helps any, but here is the code for my other functions:

    SelectionSort2
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MaxSize 20
    
    
    void SelectionSort2(employee eTable[], int numEntries, FILE *outFile){
    
                    int temp, maxPt, sortPt[MaxSize];
                    int j, k;
    
                    for(j = 0; j < numEntries; j++)
                            sortPt[j] = j;
    
                    for(j = (numEntries - 1); j > 0; j--){
                            maxPt = 0;
                            for (k = 1; k <= j; k++)
                                    if(strcmp(eTable[sortPt[k]].dept, eTable[sortPt[maxPt]].dept) > 0)
                                            maxPt = sortPt[k];
                            temp = sortPt[maxPt];
                            sortPt[maxPt] = sortPt[j];
                            sortPt[j] = temp;
                    }
    
                     fprintf(outFile, "\n\n********************SORTED DATA FROM ''C'' OPTION POINTER SORT**************************\n\n\n");
    
                    for(k = 0; k < numEntries; k++)
                            fprintf(outFile, "%-10s\t%-12s\t%-20s\t%-4.2f\t%-10s\n", eTable[k].surname, eTable[k].given, eTable[k].dept, eTable[k].payRate, eTable[k].eyeColor);
    
            }
    SelectionSort3
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MaxSize 20
    
    void SelectionSort3(employee eTable[], int numEntries, FILE *outFile){
    
                    int temp, maxPt, sortPt[MaxSize];
                    int j, k;
    
                    for(j = 0; j < numEntries; j++)
                            *(sortPt + j) = j;
    
                    for(j = (numEntries - 1); j > 0; j--){
                            maxPt = 0;
                            for (k = 1; k <= j; k++)
                                    if(strcmp((*(eTable +(*(sortPt + k)))).dept, (*(eTable + (*(sortPt + maxPt)))).dept) > 0)
                                            maxPt = *(sortPt + k);
                            temp = *(sortPt + maxPt);
                            *(sortPt + maxPt) = *(sortPt + j);
                            *(sortPt + j) = temp;
                    }
    
                     fprintf(outFile, "\n\n********************SORTED DATA FROM ''B'' OPTION ADDRESS ARITHMETIC  SORT**************************\n\n\n");
    
                    for(k = 0; k < numEntries; k++)
                            fprintf(outFile, "%-10s\t%-12s\t%-20s\t%-4.2f\t%-10s\n", (*(eTable + k)).surname, (*(eTable + k)).given, (*(eTable + k)).dept, (*(eTable + k)).payRate, (*(eTable + k)).eyeColor);
    
            }
    TemplateSelectionSort
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    template <class MyType>
    TemplateSelectionSort(struct employee eTable[], int numEntries, FILE *outFile, MyType sortBy[]){
    
                    int temp, maxPt, sortPt[MaxSize];
                    int j, k;
    
                    for(j = 0; j < numEntries; j++)
                            *(sortPt + j) = j;
    
                    for(j = (numEntries - 1); j > 0; j--){
                            maxPt = 0;
                            for (k = 1; k <= j; k++)
                                    if(strcmp((*(eTable +(*(sortPt + k)))).sortBy, (*(eTable + (*(sortPt + maxPt)))).sortBy) > 0)
                                            maxPt = *(sortPt + k);
                            temp = *(sortPt + maxPt);
                            *(sortPt + maxPt) = *(sortPt + j);
                            *(sortPt + j) = temp;
                    }
    
                     fprintf(outFile, "\n\n********************SORTED DATA FROM ''B'' OPTION ADDRESS ARITHMETIC  SORT**************************\n\n\n");
    
                    for(k = 0; k < numEntries; k++)
                            fprintf(outFile, "%-10s\t%-12s\t%-20s\t%-4.2f\t%-10s\n", (*(eTable + k)).surname, (*(eTable + k)).given, (*(eTable + k)).dept, (*(eTable + k)).payRate, (*(eTable + k)).eyeColor);
    
            }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    You can't use a . in your #define symbol. Preprocessor symbols have basically the same rules for naming as C symbols. Starts with a letter, everything after that must be a letter, digit or underscore. Try something like #ifndef lib_h__. If that doesn't fix it, I'll take a look at your other functions.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    Thanks. That did work. And i just saw that you had suggested to move the struct definition into the header and include the header in the programs, which i did. and that solved alot of problems. now I'm getting...

    [13:27:03][peg002@lnxAcadShell01][/home/peg002/LAB4]+ gcc -Wall -o lib/LAB4.o lib/LAB4.c
    In file included from lib/LAB4.c:4:
    lib/lib.h:17: error: expected declaration specifiers or ‚...‚ before ‚MyType‚
    lib/LAB4.c: In function ‚main‚:
    lib/LAB4.c:89: error: too many arguments to function ‚TemplateSelectionSort‚
    lib/LAB4.c:90: error: too many arguments to function ‚TemplateSelectionSort‚
    Last edited by philgrek; 05-06-2011 at 12:28 PM. Reason: fixed some problems..

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    This is how my header file looks now

    Code:
    [13:28:33][peg002@lnxAcadShell01][/home/peg002/LAB4]+ cat lib/lib.h
    #ifndef lib_h
    #define lib_h
    #define MaxSize 30
    
     //Define struct//
    struct employee {
            char surname[10];
            char given[12];
            char dept[20];
            float payRate;
            char eyeColor[8];
    };
    
    extern void SelectionSort(struct employee eTable[MaxSize], int, FILE*);
    extern void SelectionSort2(struct employee eTable[MaxSize], int, FILE*);
    extern void SelectionSort3(struct employee eTable[MaxSize], int, FILE*);
    extern void TemplateSelectionSort(struct employee eTable[MaxSize], int, FILE*, MyType);
    
    #endif

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    Code:
    template <class MyType>
    That is C++, not C. You're in the wrong forum for C++ help if that's what you need, but the rest looks like C. Did you "borrow" the code from somewhere? Your second error is probably because your prototype doesn't match the definition.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    I'm just trying to go by my prof's notes that he's provided. This part of the lab requires using a template. He includes both C and C++ instructions. Its my first time with templates so I don't know the difference between the 2 in regards to them

    My instructions for the template are:

    Complete the "B" Option (which is using relative addressing for the sort). Now implement yet another version of the sort routine as a template. Instantiate the template with our data type. After completing the "B" Option requirements, invoke your template sort sorting on the "Surname" field using the original data. Print the results to your report clearly indicating they are from the template sort. Now invoke your template sort again sorting on the "Department" field.
    Last edited by philgrek; 05-06-2011 at 12:40 PM.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Code:
    template <class MyType>
    That is C++, not C. You're in the wrong forum for C++ help if that's what you need, but the rest looks like C. Did you "borrow" the code from somewhere? Your second error is probably because your prototype doesn't match the definition.
    Scoop and poop coding strikes again... I HATE scoop and poop coding!

    Do these guys not realize the degree of self-sabotage they invoke when they try to cheat their way through these courses...
    Good luck keeping a job!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: array type has incomplete element type
    By gerger in forum C Programming
    Replies: 8
    Last Post: 10-05-2010, 07:40 AM
  2. invalid use of incomplete type
    By noobcpp in forum C++ Programming
    Replies: 8
    Last Post: 10-31-2008, 10:00 PM
  3. Incomplete type
    By nepper271 in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 10:54 AM
  4. Incomplete type specification
    By New++ in forum C++ Programming
    Replies: 14
    Last Post: 12-20-2004, 07:51 AM
  5. field has incomplete type
    By arcnon in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 01:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21