C exercises: Pointers, character arrays, and standard I/O exercises

These exercises are from exercise 10.3 on p. 424 and exercise 8.1 on p. 354, with minor modifications, of C How to Program (2nd ed.) by Dietel, H.M. & Dietel, P.J. (1988). Englewood Cliffs, NJ: Prentice Hall.


  1. Write a single statement or set of statements to accomplish each of the following:

    1. Define a structure called route containing an int variable routeID, and char array descrp whose values may be as long as 25 characters.

    2. Define Route to be a synonym for the type struct route.

    3. Use Route to declare variable route1 to be of type struct route, array longroutes[10] to be of type struct route, and variable routePtr to be of type pointer to struct route.

    4. Read a route id and description from the keyboard into the individual members of variable route1.

    5. Assign the member values of variable route1 to element 3 of array longroutes.

    6. Assign the address of array longroutes to the pointer variable routePtr.

    7. Print the members values of element 3 of array longroutes to the display using the variable routePtr and the structure pointer operator to refer to the members.


  2. Assume the following variables have been declared as shown.
    double taxrate = 7.3, discountrate;
    char buyer[100], seller[100];
    
    1. Declare the variable tmpPtr to be a pointer to a variable of type double.

    2. Assign the address of variable taxrate to pointer variable tmpPtr.

    3. Print the value of the variable pointed to by tmpPtr to the display.

    4. Assign the value of the variable pointed to by tmpPtr to variable discountrate.

    5. Print the value of discountrate to the display.

    6. Print the address of taxrate to the display.

    7. Print the address stored in tmpPtr to the display.

    8. Is the value printed the equal to the address of taxrate?

    9. Copy the string stored in character array buyer into character array seller.

    10. Compare the string stored in character array buyer with the string in character array seller, and print the result to the display.

    11. Append the string in character array seller to the string in character array buyer. Will this cause a run-time error?

    12. Determine the length of the string stored in character array buyer, and print the result to the display.