Skip to main content

Section 19.3 Practice: Dereferencing a Pointer

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.

Compile and run the code below in your own C environment.

#include <stdio.h>

/* Don't forget your prototype. */

int main(void) {
  int num;
  
/* Insert your code here.
 * Be sure to initialize num
 * and print its value before
 * and after the function call. */
  
  return(0);
}

void increasebyone(int *a)
{
      *a = *a + 1;
}
  • 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 = &num;
*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!