#include #include #include #define NAMELENGTH 20 struct customer { // both words are the data type char name[NAMELENGTH]; float acctbal; }; // semicolon required! // any variables included here would be global (== evil) typedef struct customer custtype; // make one word typename // problem in creating pointer to struct as a parameter type // void read(struct customer *c); // must pass by reference to change void read(custtype * cr); // must pass by reference to change /* Works, but inefficient because copies all customer data void print(custtype c) { printf("%s\t$%.2f\n", c.name, c.acctbal); } */ void print(const custtype * c) { printf("%s\t$%.2f\n", c->name, c->acctbal); } int main(void) { custtype c1, *cptr; // variables of the new type using typedef struct customer carray[5]; // array of the new type w/long name strcpy(c1.name, "Jill"); c1.acctbal = 10000; print(&c1); cptr = carray + 4; (*cptr).acctbal = 1000; cptr->acctbal = 1000; strcpy(cptr->name, "last"); strcpy(carray[0].name, "Jack"); read(&c1); print(&c1); read(&carray[2]); // read(carray+1); carray[1] = c1; for (int i = 0; i < 5; i++) print(carray + i); return 0; } //void read(struct customer * c) void read(custtype * c) { char balance[20]; printf("enter name: "); gets((*c).name); printf("enter account balance: $"); gets(balance); c->acctbal = atof(balance); }