Assignment Instructions

  1. The point (haha) of this program is to learn the basics of pointers - how to create a pointer, how to output a pointer's address, how to dereference a pointer, and how to use a dereferenced pointer to change the value of a variable.
  2. Edit, compile, and run the following program on the UH UNIX shell:
    1. Write a C program that does the following:
      1. Declare and initialize 3 variables - a char, int, and double.
      2. You can ask for input from the user,
        or just "hard code" the values. For example: char letter = 'w';
      3. Declare and initialize 3 pointers. Each pointer points to one of these 3 variables. In other words, the first pointer stores the address of the character variable. The second pointer stores the address of the integer variable. The third pointer stores the address of the double variable.
      4. Using pointers, output the HEXADECIMAL VALUE (using %p) of the address of the 3 variables by outputting the address stored in the pointer.
        In other words, output the address stored in the first pointer. Then, output the address stored in the second pointer. Then, output the address stored in the third pointer.
      5. Using pointers, output the values of the 3 variables by dereferencing the pointer.
        In other words, dereference the first pointer and display the character value. Then, dereference the second pointer and display the integer value. Then, dereference the third pointer and display the double value.
      6. Using pointers, add 3 (three) to the values of the 3 variables by dereferencing the pointers.
        In other words, dereference the first pointer and add 3 to the value and then reassign the value to the dereferenced pointer. Then, dereference the second pointer and add 3 to the value and then reassign the value to the dereferenced pointer. Then, dereference the third pointer and add 3 to the value and then reassign the value to the dereferenced pointer.
        For example, here is a parallel example using variable "x":
        x = x + 3; (Except you will be using pointers instead.)
      7. Using pointers, output the values of the 3 variables by dereferencing the pointer.
        In other words, dereference the first pointer and display the character value. Then, dereference the second pointer and display the integer value. Then, dereference the third pointer and display the double value.
      8. Now output the three variables.
        In other words, output the character variable. Then, output the integer variable. Then, output the double variable.
        Note that the values have been changed by the dereferenced pointers!!
    2. Make a simple drawing of the 3 variables and the 3 pointers in memory. Draw 3 boxes, with the names of the variables next to the boxes, and the value of the variable inside the box. Use the the initial value. (The initial values are the values of the 3 variables displayed by your program.) Draw 3 more boxes, with the names of the pointers next to the boxes, the value of the pointers inside the box, and draw an arrow from the pointer to the variable. (The pointer values are the 3 addresses which which are displayed by your program. See the diagrams in the slides for details. Scan your drawing, take photo of your drawing, make a Word file, or create a Google Drawings in Google Drive. Attach the file or share the Google Drawings with me.
    3. Email your makefile and C program and drawing to the instructor, or show it to the instructor in class.
    4. For the submission guidelines and the grading guidelines, see the syllabus.

Example I/O

This is up to you, but it should display the following:
3 addresses
3 values
3 values (plus 3)
3 values (plus 3)

For example:

Here are the address of the 3 variables which are stored in the 3 pointers:
address of pointer #1 = 0xffbffa4f
address of pointer #2 = 0xffbffa48
address of pointer #3 = 0xffbffa40

Here are the values of the 3 variables using the 3 dereferenced pointers:
dereferenced pointer #1 = B
dereferenced pointer #2 = 2
dereferenced pointer #3 = 2.345678

Now my program is adding 3 to the 3 dereferenced pointers...

Here are the updated values of the 3 variables using the 3 dereferenced pointers:
dereferenced pointer #1 = E
dereferenced pointer #2 = 5
dereferenced pointer #3 = 5.345678

Here are the updated values of the 3 variables:
character variable = E
integer variable = 5
decimal variable = 5.345678