Thread: Error from "invalid use of undefined type `struct '"

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    5

    Error from "invalid use of undefined type `struct '"

    So I'm having a bit of trouble with one of my programs.
    Some of you might be familiar with this one, ha.
    I want to keep the code as close to this as I can.

    I'm trying to combine some files into one program but it keeps giving me this compiler error.

    50 - invalid use of undefined type `struct employee'
    45 - forward declaration of `struct employee'
    and it does this twice.

    Any idea where I'm going wrong?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXNAME      40
    #define PHONEFIX     4
    #define PHONEEND     5
    
    int main()
    {
        void printEmployees(struct employee table[], int n);
        
        int i, n;
        
        struct phonenum
        {
               char    phoneArea[PHONEFIX];          //Phone area code.
               char    phoneMiddle[PHONEFIX];        //Middle 3 of phone #.
               char    phoneLast[PHONEEND];        //Last 4 of phone #.
        };
        
        struct employee
        {
               long    number;                     //Employee number.
               char    name[MAXNAME];              //Employee Name.
               struct  phonenum phone;             //Employee phone #;
               int     age;                        //Employee Age.
        };
        
        struct employee emptable[] =               //Listing of Employees.
        {
               {859630, "Windsor, Elizabeth  ",      "800", "467", "8336", 
        };
        
        printEmployees(emptable, sizeof(emptable)/sizeof(emptable[0]));
        
        
        system("pause");
    }
        
    (45)void printEmployees(struct employee table[], int n)
    {
         int i;
         
         for(i = 0; i < n; i++)
    (50)           printf("%s\n", table[i].name);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Isn't it obvious? You define the struct inside main, but expect to have it available out of it. Move those struct definitions and the function prototype before main.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    5
    Doesn't that make them global variables though? Because I really can't be using them that way.
    Last edited by HashTagTeddy; 05-10-2013 at 04:38 PM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I assume you know about the scope of variables, right? Well, the same goes for almost everything in C.
    Here you define phoneenum & employee inside main, therefore they will be unavailable once outside of main because they have gone out of scope. That is why I told you to move them outside of main.

    EDIT: Note that I'm talking about the struct definitions, not the variables of those structs
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    5
    Quote Originally Posted by GReaper View Post
    I assume you know about the scope of variables, right? Well, the same goes for almost everything in C.
    Here you define phoneenum & employee inside main, therefore they will be unavailable once outside of main because they have gone out of scope. That is why I told you to move them outside of main.

    EDIT: Note that I'm talking about the struct definitions, not the variables of those structs
    Just to make sure, there's no way to keep them inside main and pass them?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Since many want to be educated a bit on structs and I kept writing the same example again and again, I upload it. Take a look here.
    Welcome to the forum!!

    //Greaper χρόνια πολλά για το Πάσχα, με υγεία πάνω από όλα!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    5
    I'll take a look at it thanks.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    These:

    Code:
    struct phonenum
        {
               char    phoneArea[PHONEFIX];          //Phone area code.
               char    phoneMiddle[PHONEFIX];        //Middle 3 of phone #.
               char    phoneLast[PHONEEND];        //Last 4 of phone #.
        };
         
        struct employee
        {
               long    number;                     //Employee number.
               char    name[MAXNAME];              //Employee Name.
               struct  phonenum phone;             //Employee phone #;
               int     age;                        //Employee Age.
        };
    define these particular structures. They are not variables, they are user-defined types. They can be moved out of main into the global scope.

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    5
    You guys rock, after lookin at your responses it helped a ton.

    Thanks agian!
    -Ted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "invalid use of void expression" error
    By smith283 in forum C Programming
    Replies: 19
    Last Post: 09-04-2012, 09:10 AM
  2. Replies: 9
    Last Post: 04-18-2011, 11:39 AM
  3. Error msg: "invalid lvalue in assignment"
    By mike00800 in forum C Programming
    Replies: 5
    Last Post: 11-07-2007, 08:38 AM
  4. Replies: 2
    Last Post: 09-12-2006, 04:50 AM
  5. An error "invalid combination of operands and opcodes"!!
    By praseodeveloper in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 01:00 AM