/* * structure5.c - using pointers to access elements of a structure variable */ #include <stdio.h> int getans(void); // prototype getans: takes nothing, returns int int main(void) // header of funciton main { struct EmpInfo // declare struct with 6 elements { char first[20]; char last[20]; char phone[20]; char email[30]; int sal; int yearhired; }; // don't forget the semicolon int ii, jj; // declare counters struct EmpInfo emp[10]; // declare array of 20 elements, of type struct EmpInfo struct EmpInfo * pemp; // declare pointer pemp of type struct EmpInfo; pemp=emp; // initialize pemp to point to emp for(ii=0; (ii ? getans() : 1); ii++ ) // initialize ii to zero // Conditiona: evaluate if ii is TRUE // if TRUE, call getans() and evalueat returned result // else return 1, and evaluate. // If the outcome is TRUE, do block // incremnt ii { printf("Enter employee's first name: "); scanf("%s", (pemp+ii)->first); // read from stdin, store in array at &emp[ii].first[0] printf("Enter employee's last name: "); scanf("%s", (pemp+ii)->last); // read from stdin , store in arry at &emp[ii].last[0] printf("Enter employee's phone number: "); scanf("%s", (pemp+ii)->phone); // read from stdin , store in arry at &emp[ii].phone[0] printf("Enter employee's email address: "); scanf("%s", (pemp+ii)->email); // read from stdin , store in arry at &emp[ii].email[0] printf("Enter employee's salary: "); scanf("%d", &(pemp+ii)->sal); // read from stdin , store in int at &emp[ii].sal printf("Enter the year the employee was hired: "); scanf("%d", &(pemp+ii)->yearhired); getchar(); // clear input buffer } printf("\n\n"); pemp=emp; // not sure? reset pointer to .... for(jj=0; jj<ii; jj++) { printf("Employee [NAME]:%s %s\t", (pemp+jj)->first, (pemp+jj)->last); printf("[SALARY]:%d\t[YEARHIRED]:%d\n", (pemp+jj)->sal, (pemp+jj)->yearhired); printf("Employee [PHONE]:%s\t[EMAIL]:%s\n\n", (pemp+jj)->phone, (pemp+jj)->email); } return 0; } int getans(void) // header for function getans: takes nothing, returns int { char cc; printf("\nPress 'q' to quit, any other key to continue: "); cc=getchar(); if( cc=='q' ) { return 0; } else { while( getchar() != '\n'); return 1; } }