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 part containing an int variable partNumber, and char array partName whose values may be as long as 25 characters.

    2. Define Part to be a synonym for the type struct part.

    3. Use Part to declare variable a to be of type struct part, array b[10] to be of type struct part, and variable ptr to be of type pointer to struct part.

    4. Read a part number and a part name from the keyboard into the individual members of variable a.

    5. Assign the member values of variable a to element 3 of array b.

    6. Assign the address of array b to the pointer variable ptr.

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


  2. Assume the following variables have been declared as shown.
    double number1 = 7.3, number2;
    char s1[100], s2[100];
    
    1. Declare the variable dPtr to be a pointer to a variable of type double.

    2. Assign the address of variable number1 to pointer variable dPtr.

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

    4. Assign the value of the variable pointed to by dPtr to variable number2.

    5. Print the value of number2 to the display.

    6. Print the address of number1 to the display.

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

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

    9. Copy the string stored in character array s1 into character array s2.

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

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

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