/* ========================================================================== */ /* */ /* structures.c */ /* (c) 2017 K. J. Rock */ /* */ /* This shows how to create and use a structure with pointers */ /* */ /* ========================================================================== */ #include #include #include struct db // create an employee record { int id; int age; double salary; }; struct db employee[10]; // create a new database array called employee int main() { struct db *ep; // introduce a pointer to a structure // the name of an array is also its address ep = employee; // point where employee list starts // randomize by seeding from time srand((unsigned) time(NULL)); // instantiate founder's info // (create and fill the founder's employee record) employee[0].id = 42; // RIP Douglas Adams employee[0].age = rand() % 40 + 20; employee[0].salary = 25080.15; // instantiate the rest of the crew for (int i=1; i<10; i++) { employee[i].id = i; employee[i].age = rand() % 40 + 20; // no employees under 20 or over 60 employee[i].salary = (float) (rand() % 40000 + 20000); } // scan through array of structures for (int i=0; i<10; i++) { printf("Employee #%2d salary is $%6.2f \n", employee[i].id, employee[i].salary); } // step through list of structures using pointers for (int i=0; i<10; i++) // repeat 10 times because there are 10 employees { printf("Emp #%2d is %2d old earning $%6.2f \n", ep->id, ep->age, ep->salary); ep++; // step to next employee } } // For the time will come when people will not put up with sound doctrine. // Instead, to suit their own desires, they will gather around them a great number // of teachers to say what their itching ears want to hear. Timothy 4:3