Thread: Understanding Struct

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    41

    Understanding Struct

    trying to compile this example to get a better understanding of structures... but i keep getting errors.

    Code:
    #include <stdio.h>
    
    struct database{
      int id_number;
      int age;
      float salary;
    };
    
    int main()
    {
      database employee;  //There is now an employee variable that has modifiable 
                          // variables inside it.
      employee.age = 22;
      employee.id_number = 1;
      employee.salary = 12000.21;
    }
    any tips on better understanding this would be awesome. I'm trying to learn struct's!

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For better understanding (on our part), post the exact error messages next time. As for your problem:

    Code:
    struct database employee;
    Add the word struct there. database on it's own is not a valid type. You could also do the following:
    Code:
    typedef struct database    database_t;
    
    int main(void)
    {
        database_t employee;
    ...
        return 0;
    }
    typedef makes database_t a synonym for struct database. Also, it's best to state that main explicitly takes no parameters, and, since you said it would return an int, you ought to make it do so, hence the return 0.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Oonej View Post
    any tips on better understanding this would be awesome.
    Tip # 1 - If you are getting an error, and you want help on that error, tell us what error you are getting. How To Ask Questions The Smart Way

    Since this isn't C++, my guess is that your error has to do with missing the keyword struct before this line:
    Code:
    database employee;
    You need:
    Code:
    struct database employee;
    edit - just a hair too slow today on my part (there's a pun in there somewhere, but I'm not awake enough to make it)


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM