Thread: array type has incomplete element type

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CommonTater View Post
    Code:
    void SelectionSort(struct employee eTable[MaxSize], int numEntries, FILE *outFile){
    Lose the highlighted part and try it again... just use eTable[]
    I believe that's legal, with some caveats:
    C89:
    MaxSize must be a constant integer expression -- an integer literal or an expression that is an integer literal (e.g 3 + 4), or a #define of either of those.

    C99:
    MaxSize must be a constant integer expression (like C89) or
    MaxSize is an integer variable that is visible at the time of declaration (e.g. global variable/const) or
    MaxSize is an integer variable that comes before eTable in the parameter list.

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It sure don't work in my compiler....

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    Scoop and poop coding strikes again... I HATE scoop and poop coding!
    I hate it when people skim a thread until they hit some kind of OCD trigger, and then launch a screed missile, even if they have no clue what is actually going on.

    The OP is not cutting and pasting. And please: do not sabotage threads because you are bored and want attention.
    Last edited by MK27; 05-06-2011 at 12:47 PM.
    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. #19
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    In absolutely no way did I cheat or "scoop and poop". This is 100% my code, I just don't understand how to use templates so I'm trying to go off what he has in his notes, which apparently as I look through here is only a C++ definition of templates. We're given an entire notebook of his notes to use as a reference. I don't like being branded as a cheater because I used something in the wrong way. He has examples of C code mixed in with examples of C++ code, and I just didn't realize that I was looking at the C++ implementation.

  5. #20
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    How do you implement templates in C? I can't find anything other than C++

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @philgrek: I recommend going to your TA or prof for a better explanation. The instructions you posted don't really give me a clear idea of what he/she wants, and I'm not C++ expert.

    @Tater: Try the following and see if it works. I compiled with "gcc -Wall --std=c99 -pedantic main.c", no errors or warnings.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void print(int size, int x[size])
    {
        int i;
    
        for (i = 0; i < size; i++) {
            printf("%d ", x[i]);
        }
        putchar('\n');
    }
    
    int sum(int size, int x[size])
    {
        int i, sum;
    
        sum = 0;
        for (i = 0; i < size; i++) {
            sum += x[i];
        }
    
        return sum;
    }
    
    int main(void)
    {
        int size = 5;
        int array[] = {1, 2, 3, 4, 5};
    
        printf("Data: ");
        print(size, array);
        printf("Sum: %d\n", sum(size, array));
    
        return 0;
    }

  7. #22
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by philgrek View Post
    How do you implement templates in C? I can't find anything other than C++
    There are no templates in C. You can simulate them a bit with function pointers and void pointers for the data types, but it's a lot of fairly messy work. I really think you need some serious clarification from your prof on this.

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by philgrek View Post
    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.
    I haven't been following this too closely, and I'm not in your class, but probably you are supposed to do that in two separate programs, feed the data into each of them and have them produce separate but similar reports. C compilers and C++ compilers have potentially irreconcilable differences.

    However, you could use the same main() (and the source file that contains it).
    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

  9. #24
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    Unfortunately, I have to turn this in in an hour. I'm not intending to use C++ at all, entirely C. Ugh. Oh well

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by philgrek View Post
    Unfortunately, I have to turn this in in an hour.
    Whoops! I would just forget about option B then, there is no chance you are going to work that out in time anyway.
    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

  11. #26
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    Yeah, I'm just abandoning the template option. Other than that, the program runs pretty smoothly. Thanks for all the help! I really appreciate it

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by philgrek View Post
    How do you implement templates in C?
    You can't. C does not support classes, templates and a whole whack of other C++ stuff... You can write code that, in some ways, works like a lot of C++ stuff... but it's still C code.

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    @Tater: Try the following and see if it works. I compiled with "gcc -Wall --std=c99 -pedantic main.c", no errors or warnings.
    The compiler accepts it, but from what I've seen the value in the [] brackets is ignored... I could be wrong (happens sometimes).

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    I hate it when
    Have you been told today?

  15. #30
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CommonTater View Post
    The compiler accepts it, but from what I've seen the value in the [] brackets is ignored... I could be wrong (happens sometimes).
    No, you're right, at least for 1-d arrays. Like any array in a function definition, the degradation to a pointer makes the the size in the leftmost set of [ ] unnecessary (the other sizes are needed to compute the space between, e.g. &arr[0][0] and &array[1][0]). For a 1-d, there's only 1 set of [ ] and it doesn't need a size. For multi-dimensional arrays however, the variable size thing works, AFAIK. Here's a sample 2-d array program to test:
    Code:
    #include <stdio.h>
    
    void print(int size1, int size2, int x[size1][size2])
    {
        int i, j;
    
        for (i = 0; i < size1; i++) {
            for (j = 0; j < size2; j++) {
                printf("%4d ", x[i][j]);
            }
            putchar('\n');
        }
    }
    
    int sum(int size1, int size2, int x[size1][size2])
    {
        int i, j, sum;
    
        sum = 0;
        for (i = 0; i < size1; i++) {
            for (j = 0; j < size2; j++) {
                sum += x[i][j];
            }
        }
    
        return sum;
    }
    
    int main(void)
    {
        int size1 = 3;
        int size2 = 5;
        int array[3][5] = {
            { 1,  2,  3,  4,  5},
            { 6,  7,  8,  9, 10},
            {11, 12, 13, 14, 15}
        };
    
        print(size1, size2, array);
        printf("Sum: %d\n", sum(size1, size2, array));
    
        return 0;
    }

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, 11:54 AM
  4. Incomplete type specification
    By New++ in forum C++ Programming
    Replies: 14
    Last Post: 12-20-2004, 08:51 AM
  5. field has incomplete type
    By arcnon in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 02:02 AM