Pointers and structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vpawizard
    New Member
    • Nov 2006
    • 66

    #1

    Pointers and structures

    Hello,
    I am really scrwed up with the following stuff:

    struct employee
    {
    char* employee_name;
    };

    struct department
    {
    char* department_name ;
    struct employee* emp; //Collection of all employees in a department
    };

    In main(), I wrote
    struct department* company;

    I want to first enter the name of department and then names of employees in that department. I tried with pointers but I must hv messed up somewhere n getting segmentation faults. Tried using array representation e.g. company[i].emp[j].employee_name . This also failed. I used new operator but it didn't help. Please tell me how do deal such things in pointers.

    Thanks!!!
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by vpawizard
    Hello,
    I am really scrwed up with the following stuff:

    struct employee
    {
    char* employee_name;
    };

    struct department
    {
    char* department_name ;
    struct employee* emp; //Collection of all employees in a department
    };

    In main(), I wrote
    struct department* company;

    I want to first enter the name of department and then names of employees in that department. I tried with pointers but I must hv messed up somewhere n getting segmentation faults. Tried using array representation e.g. company[i].emp[j].employee_name . This also failed. I used new operator but it didn't help. Please tell me how do deal such things in pointers.

    Thanks!!!
    post your code?

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Originally posted by vpawizard
      Hello,
      I am really scrwed up with the following stuff:

      struct employee
      {
      char* employee_name;
      };

      struct department
      {
      char* department_name ;
      struct employee* emp; //Collection of all employees in a department
      };

      In main(), I wrote
      struct department* company;

      I want to first enter the name of department and then names of employees in that department. I tried with pointers but I must hv messed up somewhere n getting segmentation faults. Tried using array representation e.g. company[i].emp[j].employee_name . This also failed. I used new operator but it didn't help. Please tell me how do deal such things in pointers.

      Thanks!!!
      Hi you have declared a pointer to a structure. This is not an array of structures.
      Code:
      struct department
      {
         char* department_name;
         employee emp[10];    //Collection of all employees in a department
      };
      
      int main()
      {
         department *company = new department[6];
         strcpy(company[0].department_name, "Accounting");
         strcpy(company[0].emp[0].employee_name, "John Smith");
      }

      Comment

      • vpawizard
        New Member
        • Nov 2006
        • 66

        #4
        I figured out that I was using the pointers without initialization. Initializing them correctly solved the problems.Thanks for quick response.

        Comment

        Working...