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.
- Write a single statement or set of statements
to accomplish each of the following:
- Define a structure called part containing an
partNumber, and char array partName whose values
may be as long as 25 characters.
- Define Part to be a synonym for the type struct part.
- Use Part to declare variable a to be of type
array b to be of type struct part, and variable
ptr to be of type pointer to struct part.
- Read a part number and a part name from the keyboard into the
individual members of variable a.
- Assign the member values of variable a
to element 3 of array b.
- Assign the address of array b to the pointer variable ptr.
- 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.
Assume the following variables have been declared as shown.
double number1 = 7.3, number2;
char s1, s2;
- Declare the variable dPtr to be a pointer to a variable
of type double.
- Assign the address of variable number1 to pointer variable dPtr.
- Print the value of the variable pointed to by dPtr to the
- Assign the value of the variable pointed to by dPtr
to variable number2.
- Print the value of number2 to the display.
- Print the address of number1 to the display.
- Print the address stored in dPtr to the display.
- Is the value printed the equal to the address of number1?
- Copy the string stored in character array s1 into character array s2.
- Compare the string stored in character array s1 with
the string in character array s2, and print the result to the display.
- Append the string in character array s2
to the string in character array s1.
Will this cause a run-time error?
- Determine the length of the string stored in character array s1,
and print the result to the display.