notexOPENOverview
Notex/Languages/Practice: Intro to pointers, new, and shallow copies
04.05LESSON 05 OF 20 · 1 MIN READ

Practice: Intro to pointers, new, and shallow copies

Always free to read

These exercises will allow you to try out some of the pointer concepts that I’ve presented. The answers to the exercises are in the exercise_solutions directory.

  1. Given var p *int, write a program that prints the pointed-to value, or prints “no value” if p is nil. Make sure your program does not panic.

  2. Write a program that declares a variable i := 10. Use new to make a pointer to i and assign the pointer to a second variable, iNew. Modify the value of iNew. Guess whether the value of i changes, then print it out to check if you were correct.

  3. Repeat Exercise 2 with a variable ip and assign it the address of i. Modify the value of ip and predict whether the value of i will change. What is the difference between using new and using &?

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K