Programming in C++ (Part 8 Pointers)
Tags: cpp, beginners
Published: 2021-04-14 23:03:36

This post describes pointers in C/C++. A pointer is a variable that holds the address of another variable. Pointers are important because they allow us to use a special section of memory called the 'heap'. I discuss the different types of memory that can be used in a program (global, local, and dynamic).

* [Simple pointers](https://markm208.github.io/cppbook/chapter8/01/index.html)
* [Pointer to an object](https://markm208.github.io/cppbook/chapter8/02/index.html)
* [Vectors of pointers](https://markm208.github.io/cppbook/chapter8/03/index.html)
* [Arrays are pointers](https://markm208.github.io/cppbook/chapter8/04/index.html)
* [Passing data to functions with pointers](https://markm208.github.io/cppbook/chapter8/05/index.html)
* [Comparing pointers](https://markm208.github.io/cppbook/chapter8/06/index.html)
* [Three types of variables- global, local, and dynamic](https://markm208.github.io/cppbook/chapter8/07/index.html)
* [Dynamic variables example](https://markm208.github.io/cppbook/chapter8/08/index.html)
* [Dangling pointers and null pointers](https://markm208.github.io/cppbook/chapter8/09/index.html)
* [Dynamic array of students](https://markm208.github.io/cppbook/chapter8/10/index.html)

###Call to Action
Now that you have reviewed the guided code walk-throughs I ask that you write a program that will read a sequence of words from the keyboard and store them in a dynamic array of strings. Use the word 'quit' as the word that terminates the input. Print the words back to the screen in the order in which they were entered each on its own line. Do not store the same word twice.

Up until now the size of an array has been determined at compile time, now that you know about pointers and about the keyword `new`, write a program which is not restricted to selecting an upper bound at compile time for the number of words which can be read in.

One way to do this is to use `new` to create arrays of strings on the fly. Each time an array fills up, dynamically create an array which is twice as large, copy over the contents of the existing array to the new array, and continue (remember to `delete` the original array). Start with an array of 5 elements.

Here is an example of the output:

```
Enter in some text and end with the word quit:
This lab asks you to write a program that will read a sequence of words from the keyboard and store them in a dynamic array of strings. Use the word 'quit' as the word that terminates the input. Print the words back to the screen in the order in which they were entered each on its own line. Do not store the same word twice. quit

Doubling Array from 5 to 10
Doubling Array from 10 to 20
Doubling Array from 20 to 40
Doubling Array from 40 to 80
1. This
2. lab
3. asks
4. you
5. to
6. write
7. a
8. program
9. that
10. will
11. read
12. sequence
13. of
14. words
15. from
16. the
17. keyboard
18. and
19. store
20. them
21. in
22. dynamic
23. array
24. strings.
25. Use
26. word
27. 'quit'
28. as
29. terminates
30. input.
31. Print
32. back
33. screen
34. order
35. which
36. they
37. were
38. entered
39. each
40. on
41. its
42. own
43. line.
44. Do
45. not
46. same
47. twice.
Press any key to continue . . .
```
###Comments and Feedback
You can find all of these code playbacks in my free 'book', [An Animated Introduction to Programming in C++](https://markm208.github.io/cppbook/). I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter [@markm208](https://twitter.com/markm208).