Thread: Problem filling an array of structures

  1. #1
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    Problem filling an array of structures

    Hi all,

    I am trying to fill an array of structures like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 1000
    
    /*
     * 
     */
    
        int d, emp_num;
        
    typedef struct employee {
        int emp_index; //To assign a value to each employee for a later "pointing"
        char name[30];
        char address[100];
        int age;
        float salary;
    } emp_struct[N];
    
    // Takes imput that establishes the size of the staff
    void staff_size() {
        printf("What is the size of your staff?: ");
        scanf("%d", &emp_num);
    }
    
    //Fills staff with desired size of employees - This is the  array of structures 
    void staff_create(employee emp) {
    
        printf("Populating staff......... \n\n");
        for (d = 0; d <= emp_num; d++)
           emp[d];  //<<<< THE ERROR IS REFERS TO THIS LINE
    }
    
    int main(int argc, char** argv) {
    
    
    
    // pending other code
    
    
        return (EXIT_SUCCESS);
    }

    However the I get an error that says as follows:


    main.c:33: error: expected ')' before 'emp'

    What is it that I am doing wrong?

    Thnx
    Last edited by bluetxxth; 03-09-2010 at 03:46 AM.

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    You better follow a decent c programming book . I would refer you book by Dennis Ritche.

    You will get a good understanding on writing a proper code.

  3. #3
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    problem filling an array of structures

    Quote Originally Posted by vlrk View Post
    You better follow a decent c programming book . I would refer you book by Dennis Ritche.

    You will get a good understanding on writing a proper code.
    I have that book already in front of me (second edition the C programming language by Brian W Kernighan & Denix Ritchie.

    However it does not solve the question I have... do you know what the problem is?

    Bluetxxh

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    Code:
    // Takes imput that establishes the size of the staff
    
    //Fills staff with desired size of employees - This is the  array of structures 
    void staff_create(employee emp) {
    
        printf("Populating staff......... \n\n");
        for (d = 0; d <= emp_num; d++)
         emp[d];  
    }
    If i guess correctly you want to fill the emp structure varaible here .
    But you are not accessing the members here to fill the emp structure instead you are looping till you goto the end of maximum employees doing nothing as i see it here. Basic thing is no lvalue here for that emp[d] statement .

    Hope this observation help's you.

  5. #5
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    HI vlrk,

    I am not sure I understand. I have notice however that the problem happens when I make use of typedef and when I try to parametrize the variable emp of type employee to work with it instead of working directly with emp_struct[d]. If coding it the way I did now it works fine... at least it works the way I wanted it to.

    look at the code below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 10000 // may not be needed if array index is given explicitly
    
    //Global variables
    int d = 0, emp_num = 0;
    
    //Structure 
    
    struct employee { // <<<<<< HERE I REMOVED THE TYPEDEF KEYWORD
        int emp_index; //To assign a value to each employee for a later "pointing"
        char name[30];
        char address[100];
        int age;
        float salary;
    } emp_struct[N];
    
    
    
    // Takes imput that establishes the size of the staff
    void staff_size() {
        printf("What is the size of your staff?: ");
        scanf("%d", &emp_num);
    }
    
    //Fills staff with desired size of elements - This is the  array of structures (emp_struct)
    void staff_create() {//<<<<<<HERE I REMOVED PARAMETERS
    
        printf("Populating staff......... \n\n");
        for (d = 0; d <= emp_num; d++)
            emp_struct[d]; //<<< HERE I WORK WITH emp_struct[d] instead
    
    
    }
    Why is this happening? can you illustrate with an example which would be the correct way of making an array of structures while using typedef?


    Thank you much,

    Bluetxxth
    Last edited by bluetxxth; 03-09-2010 at 05:09 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bluetxxth
    Why is this happening? can you illustrate with an example which would be the correct way of making an array of structures while using typedef?
    This is one correct way:
    Code:
    typedef struct employee {
        int emp_index;
        char name[30];
        char address[100];
        int age;
        float salary;
    } employee;
    
    /* ... */
    
    int main(int argc, char** argv) {
        employee emp_struct[N];
        /* ... */
    Notice that emp_struct is now a local variable that you can pass appropriately to those functions that need to access it.

    There is also this option:
    Code:
    typedef struct employee employee;
    
    struct employee {
        int emp_index;
        char name[30];
        char address[100];
        int age;
        float salary;
    };
    You can define emp_struct here if you want, but generally you should not because you should normally avoid global variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    Thank you Laserlight and VRLK, I will test your suggestions further...

    Bluetxxth

  8. #8
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    Thank you Laserlight everything worked fine.

    Just out of curiosity, however... You declared the structure like this:


    Code:
    typedef struct employee {
        int emp_index;
        char name[30];
        char address[100];
        int age;
        float salary;
    } employee;

    Can I instead declare it like this?:

    Code:
    typedef struct  {// <<< REMOVED employee here
        int emp_index;
        char name[30];
        char address[100];
        int age;
        float salary;
    } employee; //<<< LEFT EMPLOYEE HERE

    In other words, why having employee at the beginning and at the end of the declaration?

    Br,

    bluetxxth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. 2d array of structures
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 03:17 PM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 05:54 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM