Skip to main content
Logo image

Section 19.2 Dereferencing a Pointer

We will now explore how the concept of pointers (addresses) can be used to modify the value of a variable inside a function, despite the fact that the variable’s scope does not extend to that function.

If you cannot see this codecast, please click here.

Check Your Understanding Check Your Understanding

1.

    You want to write a function that increases the integer value stored in a variable num by one. Given the following function definition, what is the correct function call? Assume that a variable num of type int has been declared and initialized.
    void increasebyone(int *a)
    {
      *a = *a + 1;
    }
    
    Feel free to use the window below to try out some code. Be sure to work the correct answer out "by hand" first before verifying your answer using the code window.

    admin.....open in new window

  • increasebyone(&num);
    
  • Correct
  • increasebyone(*num);
    
  • Not quite. Try again!
  • increasebyone(num);
    
  • Not quite. Try again!
  • num = increasebyone(&num);
    
  • Not quite. Try again!

2.

    What does the following piece of code do?
    float num = 5.0;
    float *pointer;
    
    pointer = #
    *pointer = *pointer + 1.0;
    printf("%f", *pointer);
    
  • This code prints out the number 6.000000 to the default output.
  • Correct
  • This code prints out the address of the variable pointer to the default output.
  • Not quite. Try again!
  • This code prints out the number 5.000000 to the default output.
  • Not quite. Try again!
  • This code prints the address of the variable num to the default output.
  • Not quite. Try again!