dynamic array and structure problem

This is a discussion on dynamic array and structure problem within the C Programming forums, part of the General Programming Boards category; Hi all, I have been trying to make a program to test pointers and structures amongst other things. I have ...

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

    dynamic array and structure problem

    Hi all,

    I have been trying to make a program to test pointers and structures amongst other things. I have tried to reduce it to the minimal expression because this is where the problem is. The code consists on a structure, three functions (one of which is just a menu).

    The two important functions are

    employee create();
    print_employee();

    The objective now is to create an dynamic array of employees and then output the information for a particular employee which I choose by pointing to it with a pointer.

    All works fine including memory allocation except that when I, for example, choose to enter two employee records and then later I want to output employee id number one, I still get the output for the last employee entered, that is, the output for employee number two. I think there is something wrong either with the variables I call or with the pointers.

    Can anybody hep? code below...

    Thank you,

    bluetxxth


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //Structure - can I not bury this inside the a function? how to access it later...?
    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;
    }employee;
    
    
    // Takes imput that establishes the size of the staff
    
    
    int staff_size(int emp_num) {
    
        printf("What is the size of your staff?: ");
        scanf("%d", &emp_num);
    
        return emp_num;
    }
    
    //Takes empoyee data input from user
     employee_create(int emp_num, employee *emp_rec, employee **emp_id) {
    
        
        printf("\nEnter employee id: ");
        scanf("%d", &emp_rec[emp_num].emp_index);
        printf("\nEnter the name of the employee: ");
        scanf("%s",emp_rec[emp_num].name);
        printf("\nEnter the address of the employee: ");
        scanf("%s", emp_rec[emp_num].address);
        printf("\nEnter the age of the employee: ");
        scanf("%d", &emp_rec[emp_num].age);
        printf("\nEnter the salary of the employee: ");
        scanf("%f", &emp_rec[emp_num].salary);
    
        //this is an array of id which takes emp_index as the index which in turn is a pointer which has a pointer with an index as its index and which accesses the emp_index value
        emp_id[emp_rec[emp_num].emp_index] = &emp_rec[emp_num];
        
    }
    
    
    void print_employee(employee **emp_id , int emp_num) {
        printf("Enter employee you want to inspect: ");
        scanf("%d",&emp_num);
        printf("\nEmployee summary: Id: %d \nName: %s \nAddress: %s \nAge: %d \nSalary: %f", emp_id[emp_num]-> emp_index, emp_id[emp_num]->name, emp_id[emp_num]->address, emp_id[emp_num]->age, emp_id[emp_num]->salary);//Prints desired employee
    
    }
    
    
    void menu() {
    
        printf("----------------------------------------\n");
        printf("|Choose amongst the following options: |\n");
        printf("----------------------------------------\n");
        printf("| 1- Enter employee data:              |\n");
        printf("| 2- inspect employees:                |\n");
        printf("----------------------------------------\n\n");
    }
    
    int main(void) {
    
        int d = 0,  emp_num =0, k;
        int option;
        employee *emp_rec;
        employee **emp_id;
        
    
       employee emp_struct; // Structure declaration
    
    
       emp_rec = malloc (sizeof(emp_struct)*emp_num); // dynamic memory allocation for an array of records
       emp_id = malloc (sizeof(emp_struct)*emp_num);// dynamic memory allocation for an array of employee ids
    
    
    //generate menu
       menu();
    
        while (option != 0) {
    
            switch (option) {
    
                case 1: printf("\nEnter employee data: ");
                         for(d = 0; d < staff_size(emp_num) ; d++){
                          employee_create(emp_num, emp_rec, emp_id);
                          break;
                            }break;
    
                
                case 2: printf("\nInspect employee: ");
                        print_employee(emp_id, k);
                        break;
                        
    
            }
    
            printf("\nSelect an option: ");
            scanf("%d", &option); //Returns to default state
    
            //exit while loop
        }
    
       free(emp_rec);
       free(emp_id);
    
        return (0);
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    For the simple reason that array indexes start at 0 not 1.
    So employee number 1 would be emp_id[emp_num-1].

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    since your emp_num is 0 you are allocating 0 bytes for your arrays. how you plan to store anything there?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43
    Excuse my delay,

    Thank you for the answer. I will test it and get back to this.

    br,

    bluexxth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  3. Structure array problem
    By Birdhaus in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2005, 08:59 PM
  4. Dynamic Structure Array Help
    By B_D_C in forum C Programming
    Replies: 8
    Last Post: 10-14-2003, 09:04 AM
  5. Dynamic array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2002, 09:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21