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.
-
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.
-
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. -
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.