Pointers, References, and the const Keyword


Pointers and the const Keyword

The const keyword frequently appears in the declaration of pointer variables. It is used to restrict what can be done with the pointer.

Things you can ordinarily do with a pointer

Let's say we have the following array declaration and pointer declaration:

    int scores[6] = {100, 95, 87, 63, 78, 89};

    int num = 10;
    int* p = #

(An array name is converted into a pointer to the first element of the array when used in a value context, so most of the things you can do with pointers can also be done with an array name.)

Here's a list of things we can do with the pointer we've declared (and - in some cases - with the array name):

  1. Dereference the pointer to get the value it points to. Examples:

        cout << *p << endl;          // Prints 10
    
        cout << *scores << endl;     // Prints 100
    
  2. Subscript the pointer to get the value it points to. Examples:

        cout << p[0] << endl;          // Prints 10
    
        cout << scores[2] << endl;     // Prints 87
    
  3. Use pointer arithmetic notation and the dereference operator with the pointer to get the value it points to. Examples:

        cout << *(p + 0) << endl;          // Prints 10
    
        cout << *(scores + 2) << endl;     // Prints 87
    
  4. Dereference the pointer to change the value it points to. Examples:

        *p = 5;          // Changes num to 5
    
        *scores = 5;     // Changes scores[0] to 5
    
  5. Subscript the pointer to change the value it points to. Examples:

        p[0] = 5;          // Changes num to 5
    
        scores[2] = 5;     // Changes scores[2] to 5
    
  6. Use pointer arithmetic notation and the dereference operator with the pointer to change the value it points to. Examples:

        *(p + 0) = 5;          // Changes num to 5
    
        *(scores + 2) = 5;     // Changes scores[2] to 5
    
  7. Change the adddress stored in the pointer (this does not work with an array name). When you add to or subtract from the address stored in a pointer variable, the arithmetic is scaled using the size of the data type the pointer points to. For example, adding 4 to a pointer to an int actually adds 16 to the address stored in the pointer (4 * sizeof(int) = 4 * 4 = 16), while adding 4 to a pointer to a char adds only 4 to the address stored in the pointer (4 * sizeof(char) = 4 * 1 = 4). Examples:

        p++;          // Adds 4 to the address in p
    
        p = p + 2;    // Adds 8 to the address in p
    
        p -= 3;       // Subtracts 12 from the address in p
    

The const keyword can be used in the declaration of a pointer variable to restrict what can be done with the pointer. There are three possible variations:

References and the const Keyword

The const keyword also frequently appears in the declaration of reference variables. It is used to restrict what can be done with the reference.

Things you can ordinarily do with a reference

Let's say we have the following reference variable declaration:

    string greeting = "hello";
    string& s = greeting;

Here's a list of things we can do with the reference variable we've declared (this is a much shorter list than what we can do with a pointer):

  1. Use the reference variable to get the value of the variable it refers to. Example:

        cout << s << endl;     // Prints "hello"
    
  2. Use the reference variable to change the value of the variable it refers to. Example:

        s = "goodbye";          // Changes greeting to "goodbye"
    

The const keyword can be used in the declaration of a reference variable to restrict what can be done with it. There's only one possible use:

There is no such thing as a "constant reference." Reference variables are effectively always constant, since once you've initialized a reference variable there's no way to make it refer to a different variable instead.