Sobes.tech
Junior — Senior

The effect of reassigning a pointer inside a function on an external pointer

livecode

Task condition

It is necessary to determine what output the program will produce and whether the reference passed to the function is changed to an object outside its scope.

package main

import "fmt"

func modifyPointer(p *int) {
    v := 3
    p = &v
}

func main() {
    x := 5
    ptr := &x
    fmt.Println(*ptr)

    modifyPointer(ptr)
    fmt.Println(*ptr)
}