Bird
Raised Fist0
Goprogramming~10 mins

Pointer declaration in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Pointer declaration
Declare variable
↓
Declare pointer variable
↓
Assign address of variable to pointer
↓
Use pointer to access value
↓
Modify value via pointer
↓
End
This flow shows how to declare a variable, then declare a pointer to it, assign the address, and use the pointer to read or change the value.
Execution Sample
Go
package main
import "fmt"
func main() {
	var x int = 10
	var p *int
	p = &x
	fmt.Println(*p)
	*p = 20
	fmt.Println(x)
}
Declare an int variable x, declare a pointer p to int, assign p the address of x, print value via pointer, change value via pointer, print x.
Execution Table
StepActionVariableValuePointer ValueOutput
1Declare xx10undefined
2Declare ppundefinednil
3Assign p = &xpundefinedaddress_of_x
4Print *px10address_of_x10
5Set *p = 20x20address_of_x
6Print xx20address_of_x20
💡 Program ends after printing updated x value 20.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
xundefined1010102020
pundefinedundefinedniladdress_of_xaddress_of_xaddress_of_x
Key Moments - 2 Insights
Why does *p print 10 at step 4 instead of the address?
Because *p means 'value at the address p points to', so it prints the value of x, which is 10 (see execution_table step 4).
Why does changing *p at step 5 change x's value?
Because p points to x's address, so *p = 20 updates x directly (see execution_table step 5 and variable_tracker for x).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of p?
Anil
Baddress_of_x
C10
Dundefined
💡 Hint
Check the 'Pointer Value' column at step 3 in execution_table.
At which step does the value of x change from 10 to 20?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Value' column for x in execution_table between steps 4 and 5.
If we print p instead of *p at step 4, what would be the output?
A20
B10
Caddress_of_x
Dnil
💡 Hint
Pointer p holds the address of x, see 'Pointer Value' column in execution_table step 4.
Concept Snapshot
Pointer declaration in Go:
var x int = 10       // declare variable
var p *int           // declare pointer to int
p = &x               // assign address of x to p
*p accesses value at p's address
Changing *p changes x's value
Full Transcript
This example shows how to declare a variable x and a pointer p to int in Go. We assign p the address of x using &x. Printing *p shows the value of x. Changing *p changes x's value because p points to x. The execution table traces each step, showing variable and pointer values and outputs. Key moments clarify why *p prints the value, not the address, and how changing *p updates x. The quiz tests understanding of pointer values and when x changes.

Practice

(1/5)
1. What does a pointer in Go store?
easy
A. The actual value of a variable
B. The memory address of a variable
C. The type of a variable
D. The size of a variable

Solution

  1. Step 1: Understand what a pointer represents

    A pointer does not hold the value itself but the location where the value is stored in memory.
  2. Step 2: Recall pointer definition in Go

    In Go, a pointer stores the address of a variable, not the variable's value directly.
  3. Final Answer:

    The memory address of a variable -> Option B
  4. Quick Check:

    Pointer = memory address [OK]
Hint: Pointers always hold addresses, not values [OK]
Common Mistakes:
  • Thinking pointers store the actual value
  • Confusing pointer with variable type
  • Assuming pointer stores variable size
2. Which of the following is the correct way to declare a pointer to an int variable named num in Go?
easy
A. var p int = &num
B. var p *int = num
C. var p *int = &num
D. var p int = *num

Solution

  1. Step 1: Understand pointer declaration syntax

    In Go, to declare a pointer to an int, use *int as the type.
  2. Step 2: Assign the address of num to the pointer

    The address operator & gets the memory address of num, which can be assigned to the pointer.
  3. Final Answer:

    var p *int = &num -> Option C
  4. Quick Check:

    Pointer declaration uses *type and &variable [OK]
Hint: Pointer type uses * before type, assign with &variable [OK]
Common Mistakes:
  • Forgetting the * in pointer type
  • Assigning variable value instead of address
  • Using *num instead of &num for assignment
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
  x := 10
  p := &x
  *p = 20
  fmt.Println(x)
}
medium
A. 20
B. 10
C. Memory address of x
D. Compilation error

Solution

  1. Step 1: Understand pointer assignment and dereferencing

    Variable p stores the address of x. Using *p = 20 changes the value at that address.
  2. Step 2: Check the effect on x

    Since p points to x, changing *p changes x to 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    Dereferencing pointer changes original variable [OK]
Hint: Changing *p changes the original variable [OK]
Common Mistakes:
  • Thinking *p creates a new variable
  • Expecting output to be address instead of value
  • Confusing pointer declaration with dereferencing
4. Identify the error in this Go code snippet:
var a int = 5
var p *int = a
fmt.Println(*p)
medium
A. Cannot assign int to *int without & operator
B. Cannot print pointer value with * operator
C. Variable a must be a pointer
D. Missing import fmt

Solution

  1. Step 1: Check pointer assignment rules

    Pointer variables must be assigned the address of a variable, not the variable's value.
  2. Step 2: Identify missing & operator

    Assigning a directly to p is invalid; it should be p = &a.
  3. Final Answer:

    Cannot assign int to *int without & operator -> Option A
  4. Quick Check:

    Pointer assignment needs &variable [OK]
Hint: Use & to assign address to pointer variables [OK]
Common Mistakes:
  • Assigning value instead of address to pointer
  • Forgetting to import fmt for Println
  • Thinking *p prints address instead of value
5. Given the code below, what will be the output?
package main
import "fmt"
func updateValue(p *int) {
  *p = *p + 5
}
func main() {
  val := 10
  updateValue(&val)
  fmt.Println(val)
}
hard
A. 10
B. Compilation error
C. Memory address of val
D. 15

Solution

  1. Step 1: Understand function parameter as pointer

    The function updateValue takes a pointer to an int and modifies the value at that address.
  2. Step 2: Trace the function call and modification

    Passing &val sends the address of val. Inside the function, *p = *p + 5 increases val by 5.
  3. Step 3: Check final print output

    After the function call, val is updated to 15, so fmt.Println(val) prints 15.
  4. Final Answer:

    15 -> Option D
  5. Quick Check:

    Pointer parameter modifies original variable [OK]
Hint: Passing &variable lets function change original value [OK]
Common Mistakes:
  • Passing value instead of address to function
  • Expecting original variable unchanged
  • Confusing pointer dereference syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard