This section contains the Golang pointers find output programs (set 2) with their output and explanations.
Program 1:
package main
import "fmt"
func main() {
var val string = "hello"
var ptr *string
ptr = &val
fmt.Printf("%s\n", *ptr)
fmt.Printf("%c\n", ptr[1])
}
Program 2:
package main
import "fmt"
func main() {
var s *int
fmt.Println("s = ", s)
}
Program 3:
package main
import "fmt"
func main() {
x := 10
var s *int
s = &x
if s == &x {
fmt.Println("True...")
} else {
fmt.Println("False...")
}
}
Answer Program 1:
Output:
Explanation:
The above program will generate a syntax error because we cannot indexing with string pointer.
Answer Program 2:
Output:
Explanation:
The variable s is uninitialized, and the uninitialized pointer variable's value is nil.
Answer Program 3:
Output:
Explanation:
Here, the variable s is an integer pointer and x is an integer variable, s is initialized with the address of x, s contains the address of x. Thus, the condition is true.
need an explanation for this answer? contact us directly to get an explanation for this answer