CS 2213/1, Spring 2005      
Quiz 3 Questions
Last Name:                            First Name:                  

  1. Pointers:

    Write lines of code to do the following, in sequence:

    1. Declare a variable x to be an int. int x;

    2. Declare a variable p to be a pointer to int. int *p;

    3. Set p to point to the variable x. p = &x;

    4. Use the variable p (and NOT the variable x) to set the variable x equal to 47. *p = 47;


  2. Swapping:

    The following program will NOT swap (interchange) the values of a and b. Add and modify the code as necessary so that the program will do the swap.


  3. Pointers and Arrays:

    Consider the following program:

    1. Rewrite the start of the while loop in line 7 so that it is shorter, but using the square bracket (array indexing) notation. while (a[i])

    2. Rewrite lines 7 and 8 to use pointer notation (no use of square brackets).
      while (*(a + i) != 0)
      sum = sum + *(a + i++);


    Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.