This content originally appeared on DEV Community and was authored by Mark Mahoney
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
- Pointer to an object
- Vectors of pointers
- Arrays are pointers
- Passing data to functions with pointers
- Comparing pointers
- Three types of variables- global, local, and dynamic
- Dynamic variables example
- Dangling pointers and null pointers
- Dynamic array of students
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++. 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.
This content originally appeared on DEV Community and was authored by Mark Mahoney
Mark Mahoney | Sciencx (2021-04-14T23:03:36+00:00) Programming in C++ (Part 8 Pointers). Retrieved from https://www.scien.cx/2021/04/14/programming-in-c-part-8-pointers/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.