Pointers  
 
 

Week 4

Readings:

For this week please read Chapter 6 in Overland on pointers.

Key Concepts:

Unlike in other languages such as Java or Python, in C++ declaring a variable does not mean declaring a pointer to a memory location with the data, but rather the data is just stored in the memory address to which the variable refers. While this has some advantages, it can make some simple things, such as writing a function to swap variable values, impossible without the use of pointers. So, unlike in other languages, an understanding of pointers in C++ is \textbf{very important}, and you should read through this chapter and do the exercises carefully. By the end you should hopefully have a sense of:

  • How to declare and use pointers in C++.
  • When pointers are needed in C++.
  • How pointer arithmetic works.

Exercises from Reading:

All the exercises from the reading.

Cryptology Program:

Up until this point in all of our cipher_function programs we have need to use a global array to keep our numbers before and after encoding. Now that you know how to pass an array into a function, however, you no longer need to do so, you can declare the array as a local variable in main, and pass it into the various cipher functions. With this in mind, write a program called cipher_functions3.cpp that changes all the functions in cipher_functions2.cpp (except the rand_0toN1 function) so that they also take a pointer to an array as an argument, and modify that array instead of the global array. Test your new functions by declaring an array as a local variable in the main function, passing it into various cipher functions (and the set_array function), and printing out results.


Files to Be Downloaded