// Flawed examples of defining and using a struct #define NAMELENGTH 20 #include #include // void read(struct customer c); // ERROR - data type not yet declared int main(void) { // definition can only be used in main struct customer { // both words are the data type char name[NAMELENGTH]; float acctbal; } c1, *cptr; // variables of the new type struct customer carray[5]; // array of the new type // c1.name = "Jill"; // ERROR - incompatible types c1.acctbal = 10000; // puts(c1); // ERROR - c1 is not a string cptr = carray+4; // make sure it points to something! // *cptr.acctbal = 1000; // compiler ERROR - binds incorrectly (*cptr).acctbal = 100; // works cptr->acctbal = 1000; // better carray[0].acctbal = 100; // read(carray[1]); puts(carray[1].name); } /* void read(struct customer c) // ERROR - passed by value { // function wouldn't affect param in main char balance[20]; printf("enter name: "); gets(c.name); printf("enter account balance: $"); gets(balance); c.acctbal = atof(balance); } */