Topic outline

  • Unit 3: Pointers and References in C++

    In this unit, students will cultivate a deeper understanding of how variables are declared and represented in memory. Students will also learn about pointers and how they can be used to reference certain memory locations.

    Completing this unit should take approximately 4 hours.

    • Upon successful completion of this unit, you will be able to:

      • interpret native C/C++ variable declarations relative to data organization and access.
        • illustrate the difference between scalars, vectors, and arrays.
        • describe the various types of scalars.
        • show how scalars, vectors, and arrays are used to organize values according to a specific multi-dimensional categorization.
        • demonstrate how the range of a numeric scalar type can be extended via compound declarations.
        • explain why scalar types may not have the same numeric range from one compiler to another on the same machine.
        • explain why scalar types may not have the same numeric range using the same source-code on different machines of the same word size.
        • choose access approaches appropriate to contiguous and non-contiguous memory cases
      • choose approaches to using pointer variables for memory access within scalars, vectors, and arrays.
        • define pointer variable.
        • relate the value of a pointer variable to the location of a variable's value in memory.
        • explain how to declare a pointer variable in C/C++.
        • explain how to get the memory location of a scalar value.
        • show how pointer math is used to access specific elements of a vector or array existing in contiguous memory.
    • 3.1: Review of C-Native Variables

      • Although this is not a language course, you should review these concepts, since an understanding this material is required before studying memory pointers. As described here:

        "In the C programming language, data types are declarations for memory locations or variables that determine the characteristics of the data that may be stored and the methods (operations) of processing that are permitted involving them."

      • Read this article. As it explains:

        "In computing, a word is the natural unit of data used by a particular processor design. A word is a fixed-sized piece of data handled as a unit by the instruction set or the hardware of the processor. The number of bits in a word (the word size, word width, or word length) is an important characteristic of any specific processor design or computer architecture."

    • 3.2: Review of Pointers

      • We touched on pointers earlier; this video reviews that material. Watch from 35:51 until the end.

    • 3.3: Pointing to Memory

      • This video expands on the discussion of pointers presented earlier in this unit.
    • 3.4: Pointers for Scalars, Vectors, and Functions

      • This article discusses scalar, vector, and function pointers. Scalars occupy varying amounts of memory. For instance, an integer may occupy four bytes and a double may occupy eight bytes. Some compilers allow for extensions, such as long int and long double. These increase the memory occupied by that type of variable, and the value's accuracy and/or numeric range. You can also use different compilers on the same machine, and the same compiler may behave differently on different machines. Always check the compiler's documentation relative to your machine. In any case, a pointer incremented by 1, for instance, will always point to the next cell of a vector or array of that pointer's type.

    • 3.5: Pointers for Arrays

      • Read this section on matrices. It shows how what we learned about pointers for vectors applies to building arrays. Essentially, we are building vectors of vectors. In the C/C++ sense, the first dimension points to the rows and the second dimension points to columns for a particular row. You will note that the vector of row pointers exists in contiguous memory, and that the columns for each individual row also exists in contiguous memory. However, it is not true that the array, as a whole, exists in contiguous memory. Thus, it is not possible to calculate a single memory pointer to a particular [r][c] cell of the array.

    • 3.6: Pointer Arithmetic

      • Read these sections. Two-dimensional matrices are very common in computer programming. This article expands on the previous one by getting specific about 2D matrix pointer arithmetic. Generalize your understanding of pointer arithmetic and you will see how it applies to data stored in contiguous memory. For instance, if you declare a pointer to a byte (unsigned char) you can march your way through any object that exists in contiguous memory.

    • 3.7: Working with Pointers

      • Work through these exercises to get some hands-on experience with pointers.

    • Unit 3 Assessment

      • Take this assessment to see how well you understood this unit.

        • This assessment does not count towards your grade. It is just for practice!
        • You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
        • You can take this assessment as many times as you want, whenever you want.