Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of pointers in C++.

Quiz Information

The Questions

  1. In order to understand pointers, remember to read their declarations ____.

    1. from left to right NO
    2. from right to left YES
    3. from top to bottom NO
    4. from bottom to top NO
    5. None of the above. NO
  2. Show examples of the four (4) different kinds of pointer const-ness. Briefly explain how each differs from the others.

    
          i) type *                nothing is const; both ends of the pointer can
                                   be changed
    
         ii) const type *          target is const; only the destination of the
             type const *          pointer can be redirected; the value there
                                   cannot be changed
    
        iii) type * const          destination is fixed/const; the value there can
                                   be changed at will, but we cannot redirect the
                                   pointer
    
         iv) const type * const    everything is const; can't move the pointer and
             type const * const    can't change the value pointed to
    
    
  3. The ____ operator is used to dereferrence a pointer (follow it to the value at its end). The ____ operator, on the other hand, is used to 'take' the address of an object (for either storing in a pointer or comparing to a pointer value).

    1. +, - NO
    2. *, / NO
    3. &, ¦ NO
    4. reference, value NO
    5. *, & YES
  4. One must show care with declaration of multiple pointers as the * (star, asterisk) used during declaration doesn't 'distribute' through the declaration. (That is, 'type *p1, p2;' would declare p1 to be a pointer but p2 would be a simple variable.) This symbol isn't an operator, btw, but simple syntax.

     

    To alleviate such easy to make logic errors, we can utilize the idea of a typedef to make the * stick to the data type.

  5.  
    TRUE   FALSE   If a pointer to a constant points to the same memory location as a pointer to a normal value, neither pointer can be used to change the location's contents. (i.e.
        type x;
        const type * p1 = &x;
        type * p2 = &x;
        // *p1 = value;    // illegal since p1 points to a constant
    
        // since p1 points to a constant, p2 won't be able to change x either
        *p2 = value;  // won't work?
    
    )
    TRUE   FALSE   If one pointer points to another pointer which in turn points to a data value, you would need only one dereference operator to reach the data from the first pointer. (i.e.
        p1                    p2                    x
         +---+                 +---+                 +---+
         |   |                 |   |                 |   |
         |  ------------------>|  ------------------>| 4 |
         |   |                 |   |                 |   |
         +---+                 +---+                 +---+
    
    )
    TRUE   FALSE   Pointers were used to effect reference arguments in C.
    TRUE   FALSE   If a function is going to change the destination/address pointed to by a pointer argument, it need not have a reference to the pointer argument. (i.e.
        ... f(type * p...)  // don't need a reference to p??
        {
            p = ...;  // f wants to point p somewhere new for its caller
        }
    
    )
  6. When using pointer arithmetic, all increments, decrements, and differences are measured in elements (not in bytes as many newbie programmers tend to believe). So, when we have a pointer to long (a long typically takes up 4 bytes of memory), a ++ of the pointer will move its target 4 (four) bytes from its original address.

  7. Show code to find the length of a C-string using no counter or index variables. (And don't call the strlen function! Do it yourself...)

    
        const char * p = cstr;
        while (*p != '\0')
        {
            ++p;
        }
        // length is p - cstr
    
    
  8. Write a loop which can uppercase every third character in a C-string. Use no index variables.

    
        char * p = cstr;
        while (*p != '\0')
        {
            *p = static_cast<char>(toupper(*p));
            ++p;
            if (*p != '\0')
            {
                ++p;
                if (*p != '\0')
                {
                    ++p;
                }
            }
        }
    
    
  9. To call a method of a class object to which we have a pointer, we should use the -> (arrow) operator.

    That is:

        Class_type * p, object;
    
        p = &object;
    
        (*p).method();   // call method via p -- nasty precedence rules!
    
        // that was horrible, how else do we call a method of 'object' from p?
    
  10. Given the following code, show what is printed in each code snippet.

        float array[10] = { 4, 8, 16, 32, 64, 128, 256, 512, 1024 };
        float *p;
    
    1.     p = array;
          cout << *p << endl;
      
          4
      
    2.     p = &(array[4]);
          cout << *p << endl;
      
          64
      
    3.     p = array+5;
          cout << *p << endl;
      
          128
      
    4.     p = array+5;
          cout << p[-1] << endl;
      
          64
      
    5.     p = array+10;
          cout << p[-1] << endl;
      
          0
      
    6.     p = array;
          p += 12;
          p -= 4;
          cout << *p << endl;
      
          1024
      
    7.     p = array+10;
          cout << p-array << endl;
      
          10
      
    8.     p = array;
          cout << *(p++) << ' ' << *p << endl;
      
          Either:  4 4
          Or:      4 8
          depending on the compiler's sequencing choices.
      
    9.     p = array;
          cout << *(p++) << ' ';
          cout << *p << endl;
      
          4 8